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
This commit is contained in:
tom_taylor 2024-09-19 03:55:25 +00:00
parent be2d9454a9
commit b349e47c31

31
make_qrcodes/make_qr.py Normal file
View File

@ -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')