Skip to content

Commit

Permalink
Merge pull request #2 from pfrydlewicz/1-adding-border-width-export-f…
Browse files Browse the repository at this point in the history
…ile-format-and-background-color

adding multiple formats and border witdth
  • Loading branch information
pfrydlewicz authored Nov 28, 2023
2 parents a13426e + 8871548 commit 12ade27
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 15 deletions.
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ms-azuretools.vscode-docker",
"ms-python.pylint",
"ms-python.black-formatter"
"demystifying-javascript.python-extensions-pack"
]
}
},
Expand Down
63 changes: 59 additions & 4 deletions qr-gen-app/interfaces/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,73 @@
from pydantic import AnyHttpUrl
from typing import Annotated

from interfaces.enums import file_formats
from interfaces.functions.encode import create_qr

router = APIRouter()


@router.get(
"/v1/encode_url",
responses={200: {"content": {"image/png": {}}}},
responses={
200: {
"content": {
file_formats.get_content_type(fmt): {}
for fmt in file_formats.FileFormatEnums
}
}
},
response_class=Response,
tags=["Encoding"],
)
async def encode_url(url: Annotated[AnyHttpUrl, Query(max_length=1024)]):
async def encode_url(
url: Annotated[AnyHttpUrl, Query(max_length=1024)],
border_width: Annotated[int, Query(ge=0, le=20)] = 2,
box_width: Annotated[int, Query(ge=1, le=20)] = 5,
file_format: file_formats.FileFormatEnums = file_formats.FileFormatEnums.PNG,
):
""""""
result_bytes = create_qr(text=str(url))
return Response(content=result_bytes, media_type="image/png")
result_bytes, exported_format = create_qr(
text=str(url),
box_width=box_width,
border_width=border_width,
file_format=file_format,
)
return Response(
content=result_bytes,
media_type=file_formats.get_content_type(exported_format),
headers={"Content-Type": file_formats.get_content_type(exported_format)},
)


@router.get(
"/v1/encode_string",
responses={
200: {
"content": {
file_formats.get_content_type(fmt): {}
for fmt in file_formats.FileFormatEnums
}
}
},
response_class=Response,
tags=["Encoding"],
)
async def encode_url(
string: Annotated[str, Query(max_length=2000)],
border_width: Annotated[int, Query(ge=0, le=20)] = 2,
box_width: Annotated[int, Query(ge=1, le=20)] = 5,
file_format: file_formats.FileFormatEnums = file_formats.FileFormatEnums.PNG,
):
""""""
result_bytes, exported_format = create_qr(
text=string,
box_width=box_width,
border_width=border_width,
file_format=file_format,
)
return Response(
content=result_bytes,
media_type=file_formats.get_content_type(exported_format),
headers={"Content-Type": file_formats.get_content_type(exported_format)},
)
Empty file.
21 changes: 21 additions & 0 deletions qr-gen-app/interfaces/enums/file_formats.py
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"
47 changes: 36 additions & 11 deletions qr-gen-app/interfaces/functions/encode.py
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

0 comments on commit 12ade27

Please sign in to comment.