Create QRCODE
import qrcode
img = qrcode.make('Binoy Thomas')
print(type(img)) # <class 'qrcode.image.pil.PilImage'>
print(img.size) # (290, 290)
img.save('QR.png')
Embed QRCode in image
import qrcode
from PIL import Image
img = Image.open('Anupam.jpg')
info = '''Actor Anupam Kher was born on 7 March 1955 in \
Shimla. He is regarded as an outstanding cineaste and is \
the recipient of 2 National Film Awards and 8 Filmfare \
Awards. Kher has appeared in over 500 films in several \
languages. His break-through performance was in Saaransh \
(1984), where he essayed the role of a septugenerian, when \
still in his 20's. His biography "Lessons Life Taught Me \
Unknowingly" is published by Penguin Random House.'''
qr = qrcode.QRCode(box_size=2)
qr.add_data(info)
qr.make()
Qr = qr.make_image()
pos = (img.size[0] - Qr.size[0], img.size[1] - Qr.size[1])
img.paste(Qr, pos)
img.save('Anupam1.png')
import qrcode
from PIL import Image
face = Image.open('AnupamSmall.jpg').crop((100, 40, 175, 125))
Qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
Qr.add_data('I am Anupam')
Qr.make()
img = Qr.make_image().convert('RGB')
pos = ((img.size[0] - face.size[0])//2, (img.size[1] - face.size[1])//2)
img.paste(face, pos)
img.save('AnupamSmall.png')
Generate QR Code within GUI
import qrcode
from tkinter import *
qr = qrcode.QRCode(version=1, error_correction=qrcode.ERROR_CORRECT_L,
box_size=10, border=4)
win = Tk()
win.geometry("500x500")
S1 = StringVar()
S1.set("https://www.google.com")
ent = Entry(win, width=30, textvariable=S1, font=("Verdana", 16, "bold"))
ent.place(x=20, y=0)
ent.focus()
def generateQRCode():
qr.add_data(S1)
qr.make() # generate QR code
img = qr.make_image()
img.save("QRCode.png")
pic = PhotoImage(file="QRCode.png")
lbl = Label(win, image=pic)
lbl.place(x=110, y=50)
lbl.config(image=pic)
lbl.image=pic
btnGenerate = Button(win, text="Generate", font=("Verdana", 16, "bold"), command=generateQRCode)
btnGenerate.place(x=185, y=400)
win.mainloop()
No comments:
Post a Comment