-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pfrydlewicz/1-adding-border-width-export-f…
…ile-format-and-background-color adding multiple formats and border witdth
- Loading branch information
Showing
5 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"""""" | ||
|
||
from enum import Enum | ||
|
||
|
||
class FileFormatEnums(str, Enum): | ||
"""Enum for exportable QR code file formats.""" | ||
|
||
PNG = "png" | ||
JPG = "jpeg" | ||
SVG = "svg" | ||
|
||
|
||
def get_content_type(format: FileFormatEnums): | ||
match (format): | ||
case FileFormatEnums.PNG: | ||
return "image/png" | ||
case FileFormatEnums.JPG: | ||
return "image/jpeg" | ||
case FileFormatEnums.SVG: | ||
return "image/svg+xml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,45 @@ | ||
import io | ||
import qrcode | ||
from qrcode import QRCode | ||
from qrcode import constants | ||
from qrcode.image.svg import SvgImage | ||
from xml.etree.ElementTree import tostring | ||
|
||
from interfaces.enums import file_formats | ||
|
||
def create_qr(text: str) -> bytes: | ||
|
||
def create_qr( | ||
text: str, | ||
file_format: file_formats.FileFormatEnums, | ||
box_width: int, | ||
border_width: int = 4, | ||
) -> bytes: | ||
"""""" | ||
qr = qrcode.QRCode( | ||
qr = QRCode( | ||
version=1, | ||
error_correction=qrcode.constants.ERROR_CORRECT_L, | ||
box_size=10, | ||
border=4, | ||
error_correction=constants.ERROR_CORRECT_L, | ||
box_size=box_width, | ||
border=border_width, | ||
) | ||
qr.add_data(text) | ||
qr.make(fit=True) | ||
|
||
img = qr.make_image(fill_color="black", back_color="white") | ||
image_factory = None | ||
save_kwargs = {} | ||
if file_format == file_formats.FileFormatEnums.SVG: | ||
image_factory = SvgImage | ||
else: | ||
save_kwargs = {"format": file_format.value} | ||
|
||
img = qr.make_image( | ||
fill_color="black", | ||
back_color="white", | ||
image_factory=image_factory, | ||
) | ||
|
||
if isinstance(img, SvgImage): | ||
result = io.BytesIO(img.to_string()).getvalue() | ||
else: | ||
img_bytes = io.BytesIO() | ||
img.save(img_bytes, **save_kwargs) | ||
result = img_bytes.getvalue() | ||
|
||
img_bytes = io.BytesIO() | ||
img.save(img_bytes, format="PNG") | ||
return img_bytes.getvalue() | ||
return result, file_format |