In This Tutorial We Create a Python script that generates a QR code from a given text and saves it as an image. The QR code should be easily scannable and display the encoded text when scanned.
Convert Text to QR Code in Python
Install qrcode first:
pip install qrcode[pil]
Then use this code:
import qrcode def txt_to_qr(txt, fn="qrcode.png"): qr=qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=4,) qr.add_data(txt) qr.make(fit=True) img = qr.make_image(fill="black", back_color="white") img.save(fn) img.show() txt_to_qr("Hello, this is a QR code!")
This generates a QR code from any text and saves it as an image.
Steps:
1. Take input text from the user.
2. Generate a QR code using a suitable Python library.
3. Save the QR code as an image file.
4. Optionally, display the generated QR code to the user.
Result: Input: "Hello, this is a QR code!" Output: A file named `qrcode.png` is generated and displayed.
The QR code, when scanned, should show:
Hello, this is a QR code!
By following this steps we can convert text to qr code in pyhton.