You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
import qrcode, os, sys |
|
from PIL import Image, ImageDraw, ImageFont, ImagePalette, ImageColor |
|
|
|
def main(id): |
|
fontHeightMax = 40 |
|
fontsize = 1 |
|
qr = qrcode.QRCode() |
|
qr.border = 2 |
|
qr.add_data(id) |
|
qr.make(fit=True) |
|
img = qr.make_image() |
|
img = img.transform((250, 294), Image.Transform.EXTENT, (0, 0, 250, 294), fillcolor='white') |
|
cwd = os.path.dirname(os.path.abspath(__file__)) |
|
fontfile = os.path.join(cwd, 'fonts', 'Helvetica.ttf') |
|
font = ImageFont.truetype(fontfile, fontsize) |
|
while font.getsize(id)[1] <= fontHeightMax and font.getsize(id)[0] <= 240: |
|
fontsize += 1 |
|
font = ImageFont.truetype(fontfile, fontsize) |
|
fontsize -= 1 |
|
|
|
captionx = (250 - font.getsize(id)[0]) / 2 |
|
draw = ImageDraw.Draw(img) |
|
draw.text((captionx, 242), id, font=font) |
|
|
|
img.convert('RGB').save(f'{id}.png') |
|
|
|
if __name__ == '__main__': |
|
if len(sys.argv) != 2: |
|
print('Usage: python gen_qrcode.py <id>') |
|
sys.exit(1) |
|
else: |
|
id = sys.argv[1] |
|
main(id) |