This repository was archived by the owner on Jun 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqr.py
More file actions
38 lines (30 loc) · 1.2 KB
/
qr.py
File metadata and controls
38 lines (30 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import base64
import io
import os
import qrcode
from PIL import Image
def qr(text, qr_color="orange", back_color="white", logo_path="./static/qrlogo.png"):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=0,
)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color=qr_color, back_color=back_color).convert("RGBA")
if logo_path and os.path.isfile(logo_path):
logo = Image.open(logo_path).convert("RGBA")
logo_width, logo_height = logo.size
img_width, img_height = img.size
scale_factor = min(img_width // 3, img_height // 3, logo_width, logo_height)
logo.thumbnail((scale_factor, scale_factor))
logo_pos = ((img_width - logo.width) // 2, (img_height - logo.height) // 2)
logo_no_alpha = Image.new("RGB", logo.size, (255, 255, 255))
logo_no_alpha.paste(logo, mask=logo.split()[3])
img.paste(logo_no_alpha, logo_pos, mask=logo.split()[3])
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
data_uri = f"data:image/png;base64,{img_str}"
return data_uri