DEV Community

jelizaveta
jelizaveta

Posted on

Create Your Own QR Code with Python — Possibly the Most Detailed Guide

Imagine this scenario: you’re in charge of designing promotional materials for an offline event, and you need to put a QR code on posters, stands, or banners. A regular black-and-white QR code works, but it feels a bit plain — you can’t show your brand colors, and there’s no logo.

Actually, generating a QR code that’s both visually appealing and practical with Python is much easier than you think. Today, we’re going to explore how.

Getting Started: One Line of Code

First, install the library you’ll need. Open your terminal and run:

pip install spire.barcode
Enter fullscreen mode Exit fullscreen mode

After installation, it’s recommended to request a temporary license from the official source. It’s not mandatory, but with a license, the generated QR code won’t have a watermark and the quality will be better.

Your First QR Code: Up and Running in 5 Minutes

Let’s start with the basics. The following code generates a QR code containing a website URL:

from spire.barcode import *

def save_image(filename, data):
    with open(filename, "wb") as f:
        f.write(data)

# Activate license (replace with your own)
License.SetLicenseKey("YOUR_LICENSE_KEY")

# Configure QR code settings
settings = BarcodeSettings()
settings.Type = BarCodeType.QRCode
settings.Data2D = "https://your-website.com"
settings.X = 3  # Controls QR code precision
settings.ShowText = False  # Hide text below

# Generate and save
generator = BarCodeGenerator(settings)
image = generator.GenerateImage()
save_image("output/MyFirstQR.png", image)
Enter fullscreen mode Exit fullscreen mode

Run this code, and you’ll find your QR code image in the output folder.

Making QR Codes More “Durable”: Error Correction Levels

QR codes have a useful feature: even if part of the code is covered or damaged, it can still be read. This is thanks to error correction. Spire.Barcode provides four levels:

Level Recovery Ability Suitable Use Case
L 7% Short URLs, numeric only
M 15% Everyday use (recommended)
Q 25% QR codes with logos
H 30% Critical info, harsh environments

If you plan to add a logo in the center of the QR code, choose at least Q or H, otherwise the blank space may affect scanning.

Set it easily like this:

settings.QRCodeECL = QRCodeECL.H  # Highest error correction
Enter fullscreen mode Exit fullscreen mode

Data Encoding Mode: Avoid Garbled Chinese

QR codes support multiple data encoding modes. Usually, “Auto” works fine, but it’s helpful to understand the options:

  • Auto : Library decides automatically, hassle-free, best for most cases
  • Numeric : Only encodes 0–9, highest efficiency, good for phone numbers or product codes
  • AlphaNumber : Supports uppercase letters, numbers, and limited symbols, good for short English text
  • Byte : Supports Chinese, Emoji, and complex content
settings.QRCodeDataMode = QRCodeDataMode.Auto  # Default is fine
Enter fullscreen mode Exit fullscreen mode

Appearance Customization: Add Your Brand Touch

Black-and-white QR codes are classic, but changing the color can make them fit your design better. Here are some common options:

# Change colors
settings.BackColor = Color.get_White()
settings.ForeColor = Color.get_DeepSkyBlue()  # Bright blue QR code

# Add border
settings.HasBorder = True
settings.BorderWidth = 1
settings.BorderColor = Color.get_Gray()

# Adjust margins (unit: module width)
settings.LeftMargin = 2
settings.RightMargin = 2
settings.TopMargin = 2
settings.BottomMargin = 2

# Set print resolution (higher DPI = clearer)
settings.DpiX = 300
settings.DpiY = 300
Enter fullscreen mode Exit fullscreen mode

Tip: Make sure the foreground and background colors have sufficient contrast, otherwise scanners may struggle. Dark foreground + light background is the safest combination.

Add Text Below the QR Code

Sometimes we want to show the encoded info or a short message under the QR code. This helps users know what the QR code is for even without scanning it.

# Change colors
settings.BackColor = Color.get_White()
settings.ForeColor = Color.get_DeepSkyBlue()  # Bright blue QR code

# Add border
settings.HasBorder = True
settings.BorderWidth = 1
settings.BorderColor = Color.get_Gray()

# Adjust margins (unit: module width)
settings.LeftMargin = 2
settings.RightMargin = 2
settings.TopMargin = 2
settings.BottomMargin = 2

# Set print resolution (higher DPI = clearer)
settings.DpiX = 300
settings.DpiY = 300
Enter fullscreen mode Exit fullscreen mode

Add a Logo: Key Step for Branding

Many brands or official accounts put a small logo in the center of their QR code. This is easy to do:

settings.SetQRCodeLogoImage("/path/to/your/logo.png")
Enter fullscreen mode Exit fullscreen mode

Just one line. Spire.Barcode automatically handles the size and placement, centering it in the QR code.

Tips:

  • Use a simple, clear logo — complex designs may affect scanning
  • Make sure the logo color contrasts with the QR code’s foreground
  • Test with a phone scanner after adding the logo

Full Example: A Beautiful QR Code with Logo

Putting it all together, a complete example looks like this:

from spire.barcode import *

def save_image(filename, data):
    with open(filename, "wb") as f:
        f.write(data)

License.SetLicenseKey("YOUR_LICENSE_KEY")

settings = BarcodeSettings()
settings.Type = BarCodeType.QRCode
settings.Data2D = "https://your-site.com/special-offer"
settings.QRCodeDataMode = QRCodeDataMode.Auto
settings.QRCodeECL = QRCodeECL.Q  # Increase error correction for logo
settings.X = 4

# Appearance
settings.BackColor = Color.get_White()
settings.ForeColor = Color.get_DarkOrange()
settings.HasBorder = True
settings.BorderWidth = 0.5

# Add text below
settings.ShowBottomText = True
settings.BottomText = "Scan to get your coupon"
settings.SetBottomTextFont("Microsoft YaHei", 12, FontStyle.Bold)

# Add logo
settings.SetQRCodeLogoImage("logo.png")

# Generate
generator = BarCodeGenerator(settings)
image = generator.GenerateImage()
save_image("output/PromoQR.png", image)
Enter fullscreen mode Exit fullscreen mode

Final Notes

QR codes seem simple, but making them effective requires attention to a few details:

  • Error Correction : Choose at least Q if adding a logo
  • Color Contrast : Dark foreground, light background
  • Resolution : At least 300 DPI for print; 72–150 DPI is enough for screens
  • Testing : Scan with a phone before using

Spire.Barcode handles most of the underlying logic, so we can focus on style and content. Whether for event materials, product packaging, or internal systems, this solution should cover your needs.

Hope this guide helps. Feel free to share questions in the comments!

Top comments (0)