How to Generate QR Codes in Python (A Comprehensive Guide)

Alice Yang
6 min readFeb 19, 2024

--

In today’s digital age, QR codes have emerged as a powerful and versatile tool for sharing information, promoting businesses, and enhancing user experiences. These matrix barcodes, first developed in Japan, have gained widespread popularity due to their ease of use and ability to store a vast array of data. Whether it’s for business purposes, event promotions, or personal use, generating QR codes has become an essential skill for individuals and organizations alike. In this article, we will explore how to generate QR codes using Python.

We will discuss the following topics:

QR Code Generator in Python

To generate QR Codes in Python, we can use Spire.Barcode for Python library.

Spire.Barcode for Python is an easy-to-use barcode library designed for developers to generate and read 1D and 2D barcodes within Python applications. It supports a wide range of barcode types, including QR code, PDF417, Data Matrix, Aztec, Code 11, Code 25, Code 39, Codabar, Code 93, Code 128, EAN 8, EAN 13, EAN 14, EAN 128, ITF 6, ITF 14, MSI, UPCA, UPCE, USPS, RSS 14, OPC, and many more.

You can install Spire.Barcode for Python from PyPI by running the following command in your terminal:

pip install Spire.Barcode

Generate QR Code in Python

Generating a QR code is easy with Spire.Barcode for Python. Simply create an object of the BarcodeSettings class to store QR code settings, such as the barcode type (QR code), the data you want to encode, the width of the bar module, and the error correction level. After that, use the library’s BarCodeGenerator class to generate a QR code image based on your settings. Finally, save the image to a file for further use.

Here is a simple example that shows how to generate a QR code using Python and Spire.Barcode for Python:

from spire.barcode import *

# Apply a license key if you have one (you can request one by clicking the link at the end of the article)
License.SetLicenseKey("license-key")

# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type as QR code
barcodeSettings.Type = BarCodeType.QRCode
# Set the data of the QR code
barcodeSettings.Data = "12345ABCDE"
barcodeSettings.Data2D = "12345ABCDE"
# Set the width of the QR code bar module
barcodeSettings.X = 3
# Set the error correction level of the QR code
barcodeSettings.QRCodeECL = QRCodeECL.M
# Set text visibility
barcodeSettings.ShowText = False

# Create a BarCodeGenerator object with the specified settings
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate QR code image
barcodeimage = barCodeGenerator.GenerateImage()

# Save the QR code image to a .png file
with open("QRCode.png", "wb") as file:
file.write(barcodeimage)
Generate QR Code in Python
Generate QR Code in Python

Generate QR Code with Logo Image in Python

In addition to the basic QR code generation, you can also add a logo image to the QR code. This can be useful when you want to brand your QR code or make it visually appealing. To generate a QR code with a logo image in Python, you need to specify the path to the logo image file and set it as the QR code’s logo image using the SetQRCodeLogoImage() function of the BarcodeSettings class.

Here is a simple example that shows how to generate a QR code with a logo image using Python and Spire.Barcode for Python:

from spire.barcode import *

# Apply a license key if you have one (you can request one by clicking the link at the end of the article)
License.SetLicenseKey("license-key")

# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type as QR code
barcodeSettings.Type = BarCodeType.QRCode
# Set the data of the QR code
barcodeSettings.Data = "https://twitter.com"
barcodeSettings.Data2D = "https://twitter.com"

# Set the logo image of the QR code
barcodeSettings.SetQRCodeLogoImage("Logo.png")

# Set the width of the QR code bar module
barcodeSettings.X = 3
# Set the error correction level of the QR code
barcodeSettings.QRCodeECL = QRCodeECL.M
# Set text visibility
barcodeSettings.ShowText = False

# Create a BarCodeGenerator object with the specified settings
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate QR code image
barcodeimage = barCodeGenerator.GenerateImage()

# Save the QR code image to a .png file
with open("QRCodeWithLogoImage.png", "wb") as file:
file.write(barcodeimage)
Generate QR Code with Logo Image in Python
Generate QR Code with Logo Image in Python

Generate QR Code with Text in Python

Sometimes, it’s helpful to add text to a QR code to provide additional information or context. You can customize the QR code by adding top and bottom text. This allows you to include relevant details, such as a title or description, in the QR code itself. With Spire.Barcode for Python, you can set the top and bottom text, as well as customize the text color.

Here is a simple example that shows how to generate a QR code with top and bottom text using Python and Spire.Barcode for Python:

from spire.barcode import *

# Apply a license key if you have one (you can request one by clicking the link at the end of the article)
License.SetLicenseKey("license-key")

# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type as QR code
barcodeSettings.Type = BarCodeType.QRCode
# Set the data of the QR code
barcodeSettings.Data = "12345ABCDE"
barcodeSettings.Data2D = "12345ABCDE"
# Set the width of the QR code bar module
barcodeSettings.X = 3
# Set the error correction level of the QR code
barcodeSettings.QRCodeECL = QRCodeECL.M

# Set the top and bottom text
barcodeSettings.TopText = "Top Text"
barcodeSettings.TopTextColor = Color.get_Blue()
barcodeSettings.BottomText = "Bottom Text"
barcodeSettings.BottomTextColor = Color.get_Blue()

# Set text visibility
barcodeSettings.ShowText = False
barcodeSettings.ShowTopText = True
barcodeSettings.ShowBottomText = True

# Create a BarCodeGenerator object with the specified settings
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate QR code image
barcodeimage = barCodeGenerator.GenerateImage()

# Save the QR code image to a .png file
with open("QRCodeWithText.png", "wb") as file:
file.write(barcodeimage)
Generate QR Code with Text in Python

Customize the Appearance of QR Code in Python

Apart from adding a logo image or text, you can further customize the appearance of the QR code. This includes setting the background color, adding a border, adjusting the image width and height, and more. By customizing these visual aspects, you can create QR codes that align with your branding or design preferences.

Here is a simple example that shows how to generate a QR code with a customized appearance using Python and Spire.Barcode for Python:

from spire.barcode import *

# Apply a license key if you have one (you can request one by clicking the link at the end of the article)
License.SetLicenseKey("license-key")

# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()
# Set the barcode type as QR code
barcodeSettings.Type = BarCodeType.QRCode
# Set the data of the QR code
barcodeSettings.Data = "12345ABCDE"
barcodeSettings.Data2D = "12345ABCDE"
# Set the width of the QR code bar module
barcodeSettings.X = 3
# Set the error correction level of the QR code
barcodeSettings.QRCodeECL = QRCodeECL.M
# Set text visibility
barcodeSettings.ShowText = False

# Set background color
barcodeSettings.BackColor = Color.get_Cornsilk()

# Set border
barcodeSettings.HasBorder = True
barcodeSettings.BorderColor = Color.get_Blue()
barcodeSettings.BorderDashStyle = 0
barcodeSettings.BorderWidth = 1

# Set QR code image width and height (the unit is millimeter)
barcodeSettings.AutoResize = False
barcodeSettings.ImageWidth = 80
barcodeSettings.ImageHeight = 80

# Create a BarCodeGenerator object with the specified settings
barCodeGenerator = BarCodeGenerator(barcodeSettings)
# Generate QR code image
barcodeimage = barCodeGenerator.GenerateImage()

# Save the QR code image to a .png file
with open("CustomizedQRCode.png", "wb") as file:
file.write(barcodeimage)
Generate QR Code with Custom Appearance in Python
Generate QR Code with Custom Appearance in Python

Get a Free License

The library will generate an evaluation watermark on the generated QR code image, if you would like to remove the watermark, you can request a free trial license.

Conclusion

This article demonstrated 4 different scenarios for generating QR codes using Python. We hope you find it helpful.

See Also

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.