In this post, we will create a simple QR Code generator using Python. QR Codes (Quick Response Codes) are two-dimensional barcodes that can store a variety of information, such as URLs, text, or contact information. With just a quick scan using a smartphone camera, users can access the embedded information.
Table of Contents
Prerequisites
Before we start coding, make sure you have the following:
- Python Installed: Ensure that you have Python installed on your machine. You can download it from python.org.
- Libraries: We will use the
qrcode
andPillow
libraries. You can install these libraries using pip. Open your terminal or command prompt and run the following command:
pip install qrcode[pil]
Code for QR Code Generator using Python
Below is the complete code for generating a QR Code:
import qrcode from PIL import Image def generate_qr_code(data, filename): # Create a QR Code instance qr = qrcode.QRCode( version=1, # Controls the size of the QR Code error_correction=qrcode.constants.ERROR_CORRECT_L, # Error correction level box_size=10, # Size of each box in pixels border=4, # Thickness of the border (minimum is 4) ) # Add data to the QR Code qr.add_data(data) qr.make(fit=True) # Fit the QR Code to the data # Create an image from the QR Code instance img = qr.make_image(fill_color="black", back_color="white") # Save the image to a file img.save(filename) print(f"QR Code generated and saved as {filename}") if __name__ == "__main__": # Data to be encoded in the QR Code data = "https://getprojects.org" # Filename for the generated QR Code image filename = "qrcode.png" # Generate the QR Code generate_qr_code(data, filename)
Code Breakdown – QR Code Generator using Python
- Importing Libraries: We import the
qrcode
library for generating QR Codes andImage
fromPillow
for image handling. - Function Definition: The
generate_qr_code
function takes two parameters:data
(the information to encode) andfilename
(the name of the file to save the QR Code image). - QR Code Configuration:
version
: Controls the size of the QR Code (1 is the smallest).error_correction
: Defines the error correction capability. Higher levels allow the QR Code to be read even if partially damaged.box_size
: Determines the size of each individual box in the QR Code.border
: Specifies the thickness of the border around the QR Code.
- Adding Data: The
add_data
method is used to embed the specified data into the QR Code. - Creating the Image: The
make_image
method generates the QR Code image with specified fill and background colors. - Saving the Image: The generated QR Code is saved as a PNG file using the
save
method. - Main Block: In the
if __name__ == "__main__":
block, we specify the data (in this case, the URL “https://getprojects.org“) and the filename for the QR Code image. Finally, we call thegenerate_qr_code
function to create the QR Code.
Read also: Simple Chat Application in Python
Conclusion
In this tutorial, we successfully created a QR Code generator using Python. You can customize the data
variable to encode different information, such as text or other URLs. This simple tool can be very useful for sharing links, contact information, or any other data that can be encoded in a QR Code.
Feel free to explore more features of the qrcode
library and enhance your QR Code generator further! If you have any questions or suggestions, please leave a comment below.
Happy coding!