|
@@ -2,8 +2,7 @@ from io import BytesIO
|
|
from typing import Union
|
|
from typing import Union
|
|
from qrcode import QRCode
|
|
from qrcode import QRCode
|
|
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M, ERROR_CORRECT_Q, ERROR_CORRECT_H
|
|
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M, ERROR_CORRECT_Q, ERROR_CORRECT_H
|
|
-from qrcode.image.styledpil import StyledPilImage
|
|
|
|
-from qrcode.image.svg import SvgPathImage
|
|
|
|
|
|
+from qrcode.image.svg import SvgPathFillImage
|
|
from qrcode.image.styles.moduledrawers.svg import SvgCircleDrawer
|
|
from qrcode.image.styles.moduledrawers.svg import SvgCircleDrawer
|
|
from qrcode.image.styles.colormasks import RadialGradiantColorMask
|
|
from qrcode.image.styles.colormasks import RadialGradiantColorMask
|
|
|
|
|
|
@@ -13,8 +12,13 @@ QR_MAX_BYTES = {
|
|
ERROR_CORRECT_M: 2331,
|
|
ERROR_CORRECT_M: 2331,
|
|
ERROR_CORRECT_L: 2953,
|
|
ERROR_CORRECT_L: 2953,
|
|
}
|
|
}
|
|
-def get_qr_code(data: Union[bytes, str], fallback: Union[bytes, str] = None):
|
|
|
|
- err_lvl = ERROR_CORRECT_H
|
|
|
|
|
|
+QR_QUALITY_MAP = {
|
|
|
|
+ q: locals().get(f'ERROR_CORRECT_{q}') for q in (
|
|
|
|
+ 'H', 'Q', 'M', 'L'
|
|
|
|
+ )
|
|
|
|
+}
|
|
|
|
+def get_qr_code(data: Union[bytes, str], fallback: Union[bytes, str] = None, err_lvl='H'):
|
|
|
|
+ err_lvl = QR_QUALITY_MAP.get(err_lvl, err_lvl)
|
|
qr = QRCode(error_correction=err_lvl)
|
|
qr = QRCode(error_correction=err_lvl)
|
|
if data is not None and isinstance(data, str):
|
|
if data is not None and isinstance(data, str):
|
|
data = data.encode('utf-8')
|
|
data = data.encode('utf-8')
|
|
@@ -25,7 +29,7 @@ def get_qr_code(data: Union[bytes, str], fallback: Union[bytes, str] = None):
|
|
else:
|
|
else:
|
|
qr.add_data(data or fallback, optimize=0)
|
|
qr.add_data(data or fallback, optimize=0)
|
|
|
|
|
|
- img_1 = qr.make_image(image_factory=SvgPathImage)
|
|
|
|
|
|
+ img_1 = qr.make_image(image_factory=SvgPathFillImage)
|
|
with BytesIO() as f:
|
|
with BytesIO() as f:
|
|
img_1.save(f)
|
|
img_1.save(f)
|
|
f.flush()
|
|
f.flush()
|