Newer
Older
import qrcode #Used to generate the QR
import base64 #Used to encode the generated QR as base64, to directly insert it into the HTML
from io import BytesIO #Needed for base64 encoding of the image
from PIL import Image #Needed for QR generation
def makeQR(text): #This function is used to create a QR code and encode it base64, if you make a new shortlink
qr = qrcode.QRCode( #QR generation variables
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=1,
)
qr.add_data(text) #The URL is in the text variable
qr.make(fit=True) #Generate the QR
img = qr.make_image(fill_color="black", back_color="white") #Encode the WR as base 64
with BytesIO() as buffer:
img.save(buffer, 'jpeg')
return base64.b64encode(buffer.getvalue()).decode()