Introduction:
In this posts let’s learn how to create text to OR code converter using python. Text to QR code convert converts text into QR code. This program takes some input from the user and this program converts that input into QR code. QR code stores that input. If we scan that QR code using mobine phone then we san see the actuall text. Let’s learn step by step.
Full Code:
import qrcode from PIL import Image def QRcode_converter(txt): qr=qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10,border=4,) qr.add_data(txt) qr.make(fit=True) img=qr.make_image(fill_color="Black",back_color="white") img.show() txt=input("Enter some Text:") QRcode_converter(txt)
Import qrcode Function And Image Function From PIL
import qrcode from PIL import Imagge
First import the qrcode, this helps to generate the QR code. After this import Image from PIL, this helps to manipulate the image.
Defining The Function:
def QRcode_converter(txt): qr=qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10,border=4,) qr.add_data(txt) qr.make(fit=True) img=qr.make_image(fill_color="Black",back_color="white") img.show()
After importing defining a QRcode_converter(txt) function. This function takes txt as a parameter. qr=qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4,). This line code contain a variable qr which stores the qrcode.QRCode which is used in the script to create and configure the QR code. After this i give the version=1, you can give any version based on which type of QR code you are creating. error_correction controls the error correction level, which defines how much of the QR code can be destroyed. Errors are three types, that are high, medium and low level. box_size=10 defines the size of each square in the QR code. border=4 defines the width of the border around the QR code.
After this add the data to the QR Code using qr.add_data(txt). To finalizing the QR Code use the make() function ensures that the QR code fits the data, adjusting its size if necessay: qr.make(fit=True). Finally, the make_image() method generates an image object from the QR code: img=qr.make_image(fill_color=”Black”, back_color=”white”). fill_color=”black” is the actual color of the QR Code. back_color=”white” is the background color of the QR code. After all i use img.show(), which opens the image in the default image viewer of operating system.
Taking Imput From The User
txt=input("Enter some Text:")
Calling the Function:
QRcode_converter(txt)
After completing the function defination, the next step is calling the function.
Output:
Enter some Text: Hello World