QRCodex/QRCodex.py

62 lines
2.1 KiB
Python
Raw Normal View History

2025-02-28 00:22:26 +00:00
import io
# Importing Image and ImageFont, ImageDraw module from PIL package
from PIL import Image, ImageFont, ImageDraw
#import QRcode generator lib
import qrcode
#set the text and the code here
text = "█SLHJD!■C■KOQFHY■PKJSOOFH■BY■ONDTTX■GRC■ADLYJ■ZBSH■SOITRM●JKUHSOK●ZA■MAOLH■RVHRPJ!■█"
code = "TCHERNOBYL" #3💪+1👒+2👕+1🧥+1🧣+1🔫+4👂=🤠+❓TCHERNOBYL
textsize = 30 #px
#generate QRCode
qr = qrcode.QRCode(
version=1, #smallest
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=0,
)
qr.add_data(code)
#saving the normal QRCode image
2025-02-28 00:36:45 +00:00
qrcodeimage = qr.make_image()
type(qrcodeimage) # qrcode.image.pil.PilImage
qrcodeimage.save("qrcode.png")
2025-02-28 00:22:26 +00:00
#storing the qrCode in a list[list[bool]]
boolmatrix = qr.get_matrix()
# creating a image object
codeximage = Image.new("RGB", (textsize*len(boolmatrix), textsize*len(boolmatrix)), "white")
codexdraw = ImageDraw.Draw(codeximage)
2025-02-28 00:36:45 +00:00
# specify fonts
2025-02-28 00:22:26 +00:00
fontnormal = ImageFont.truetype(r'./FiraMono_Regular.otf', textsize)
fontbold = ImageFont.truetype(r'./FiraMono_Bold.otf', textsize)
2025-02-28 00:36:45 +00:00
# drawing text, one character per cell
2025-02-28 00:22:26 +00:00
index = 0
for i, inner_list in enumerate(boolmatrix):
for j, element in enumerate(inner_list):
# get the letter in the text
letter = text[index%len(text)]
# set the font and the color according to the QRCode cell bool value
if(element):
font=fontbold
color="black"
else:
font=fontnormal
2025-02-28 00:26:42 +00:00
color="grey"
# drawing a black box if the letter is █
2025-02-28 00:22:26 +00:00
if (letter == ''):
2025-02-28 00:26:42 +00:00
if (element):
codexdraw.rectangle([(j*textsize, i*textsize), (j*textsize+textsize, i*textsize+textsize)] , fill =color)
2025-02-28 00:22:26 +00:00
# drawing the letter
else:
codexdraw.text((j*textsize+textsize/5, i*textsize-textsize/10), letter, fill =color, font =font, spacing=0, align ="left")
2025-02-28 00:22:26 +00:00
index+=1
#saving the codex QRCode image with the text in the cells
codeximage.save("codex.png")