QRCodex/QRCodex.py

65 lines
2.2 KiB
Python

import io
import random
import string
# 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!■█"
text = ''.join(random.choices(string.ascii_uppercase + string.digits, k=441))
code = "📜📐✂️🕳️🔎🔠" #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=1,
)
qr.add_data(code)
#saving the normal QRCode image
qrcodeimage = qr.make_image()
type(qrcodeimage) # qrcode.image.pil.PilImage
qrcodeimage.save("qrcode.png")
#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)
# specify fonts
fontnormal = ImageFont.truetype(r'./FiraMono_Regular.otf', textsize)
fontbold = ImageFont.truetype(r'./FiraMono_Bold.otf', textsize)
# drawing text, one character per cell
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
color=(64,64,64)
# drawing a black box if the letter is █
if (letter == ''):
if (element):
codexdraw.rectangle([(j*textsize, i*textsize), (j*textsize+textsize, i*textsize+textsize)] , fill =color)
# drawing the letter
else:
codexdraw.text((j*textsize+textsize/5, i*textsize-textsize/10), letter, fill =color, font =font, spacing=0, align ="left")
index+=1
#saving the codex QRCode image with the text in the cells
codeximage.save("codex.png")