How To Generate QR Codes in Python
charly chris-jordan A Morgan
Comptable fiscaliste/Chef d'Entreprise---Développeur Web (Python,Ruby,PHP)
In today’s digital world, QR (Quick Response) codes have become an essential tool for sharing information quickly and conveniently. Their ease of use and ability to quickly connect physical objects to digital information make them an indispensable tool in today’s digital world.
QR codes can be useful across multiple industries and scenarios. It’s uses include — but is not limited to — the following:
Whether you’re a developer or an enthusiast looking to integrate QR codes into your project, Python offers a multitude of libraries and tools to help achieve this seamlessly. In this article, I would walk you through the process of effortlessly generating QR codes using the qrcode package. Let’s dive in.
Demo and Source Code
The web demo of this project can be found here. The source code can be found here. The Web Demo was created with Python (Django)
Requirements
You need to have Python version 3.7 or newer installed.
Install the qrcode package
Install the qrcode package using Python’s package manager:
pip install qrcode
A standard install — like the one above — uses pypng to generate PNG files. However, for more functionality, install qrcode with the Pillow dependency using the command below:
pip install qrcode[pil]
Import necessary module
Create and open a Python file, then import the qrcode module:
import qrcode
Generate the QR Code
link = "https://delighto.medium.com/"
qr = qrcode.QRCode(
version=1,
box_size=10,
border=4,
)
qr.add_data(link)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save('qr_code.png')
Code Breakdown
The link variable stores the data which you want to encoded into your QR code. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols)
A QR code instance is created. The version parameter is passed to it, which is an integer from 1 to 40 that determines the size of the QR code.
领英推荐
The box_size parameter controls how many pixels each square in the QR code is.
The add_data method is used to add the data to be encoded into the QR code. To add a new data, first use the clear() method to remove the existing data.
The border parameter controls the distance between the edge of the image and the QR code itself. The default value is 4.
The make_image method contains the fill colour and the background colour of the QR code. The defaults are “black” and “white” respectively. This can also be a RGB colour representation.
Save the image into a file using the save method. Pass the filename as an argument to this method.
Open the new image file and you should see the following output:
Use a QR code scanner of your choice to scan the above code and you should be directed to https://delighto.medium.com as specified in the code above.
Advanced Usage
Colour Gradient
Your QR code can be styled to have a colour gradient instead of a plain solid colour. Import the StyledPilImage and RadialGradiantColorMask class, then update the make_image method:
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.colormasks import RadialGradiantColorMask
img = qr.make_image(image_factory=StyledPilImage, color_mask=RadialGradiantColorMask())
The following parameters can be passed into the RadialGradiantColorMask class to modify its appearance: back_color, center_color, edge_color.
Embedding an Image
from qrcode.image.styledpil import StyledPilImage
img = qr.make_image(image_factory=StyledPilImage, embeded_image_path="/path/to/image.png")
Rounded Corners
The QR code’s square boxes can also be styled to have a border radius. Import the StyledPilImage and RoundedModuleDrawer class, and update the make_image method:
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
img = qr.make_image(image_factory=StyledPilImage, module_drawer=RoundedModuleDrawer())
Conclusion
We’ve explored the various aspects of creating QR codes, from choosing the right generator tool to customizing the design. By following the steps outlined in this guide, you can confidently generate QR codes tailored to your specific needs.
Whether you’re creating QR codes for websites, contact information, Wi-Fi access, or something entirely unique, the principles we’ve discussed here will serve as a solid foundation for your QR code generation endeavors.
Thanks for reading.