From b349e47c3138dcb29c86546ff2e3424a0c9312d8 Mon Sep 17 00:00:00 2001 From: tom_taylor Date: Thu, 19 Sep 2024 03:55:25 +0000 Subject: [PATCH] code to create qr codes this code creates jpg and png image qrcodes for three digit serial numbers and appends below the serial number at the qr code for ease of both machine and human interpretation. As set up it gives the 200 qr codes for str(100) through str(299), because I didn't need anymore than that but it would be easy to modify --- make_qrcodes/make_qr.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 make_qrcodes/make_qr.py diff --git a/make_qrcodes/make_qr.py b/make_qrcodes/make_qr.py new file mode 100644 index 0000000..ddb9239 --- /dev/null +++ b/make_qrcodes/make_qr.py @@ -0,0 +1,31 @@ +def make_qrcode(some_text, directory) : + '''this turns some text data into a qrcode''' + import qrcode + from PIL import Image, ImageDraw, ImageFont + from os import chdir, listdir + chdir(directory) + if type(some_text) != str : + print('input', some_text, 'is not a string') + raise TypeError + myfont = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf',40) + qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_Q, box_size=10, border=1) + print('data=',some_text) + qr.add_data(some_text) + qr.make() + img = qr.make_image() + img_w, img_h = img.size + background = Image.new('RGB',(290,290),(255,255,255)) + bg_w, bg_h = background.size + offset = ((bg_w - img_w) // 2, 0) + background.paste(img, offset) + I1 = ImageDraw.Draw(background) + I1.text((120,250),str(some_text),font=myfont,fill=0) + print(type(background)) + background.save(some_text + '.jpg') + background.save(some_text + '.png') + return + +if __name__ == '__main__' : + for indx in range(100,400) : + name = str(indx).zfill(3) + make_qrcode(name, '/home/tomtaylor/exams_/qrcodes')