How to Create Stunning QR Codes with Python

QR codes have become ubiquitous in recent years as a convenient way to share information. From website URLs to contact details to WiFi access credentials, QR codes allow smartphone users to quickly scan and access data without typing anything. Creating your own custom QR codes might seem daunting at first, but with the help of Python and the excellent segno library, you‘ll be generating stunning, functional QR codes in no time!

In this comprehensive guide, we‘ll walk through everything you need to know to create amazing QR codes using Python. We‘ll cover:

  • Installing and using the segno library
  • Generating basic QR codes and customizing their appearance
  • Saving QR codes in various formats
  • Creating QR codes for specific real-world use cases
  • Best practices for reliable QR codes

By the end, you‘ll have all the tools and knowledge necessary to make your own eye-catching, practical QR codes with ease. Let‘s get started!

Getting Started with Segno

Segno is a powerful open-source Python library for creating QR codes. It supports generating standard QR codes as well as compact micro QR codes. One of the great things about segno is that it has no external dependencies, so you can create QR codes in pure Python without relying on any other libraries.

Segno officially supports Python versions 2.6 through 3.4, though later versions should work as well. To install segno, simply use pip:

pip install segno

Once installed, we can import segno in our Python code and start generating QR codes!

Creating Your First QR Code

Let‘s create a basic "Hello World" QR code using segno. All we need is one line of code:

import segno

qr = segno.make(‘Hello World‘)
qr.save(‘hello-world.png‘)

This creates a QR code containing the text "Hello World" and saves it to a PNG image file named hello-world.png. Here‘s what the result looks like:

Hello World QR Code

That‘s it – we‘ve created our first QR code in Python using segno! The segno.make() function generates the QR code, and the save() method writes it out to an image file. By default, segno creates a crisp, clean black-and-white QR code. In the next section, we‘ll see how to customize the appearance of our QR codes.

Customizing QR Code Appearance

While the default look of segno QR codes is perfectly functional, we can make them more visually appealing by tweaking a few options. Segno provides parameters to customize the border size, scale, and module colors.

To add a border around the QR code, we use the border parameter with the save() method:

import segno

qr = segno.make(‘Example.com‘)
qr.save(‘example-com.png‘, border=4)

Example.com QR Code with Border

This adds a 4-module wide border around the QR code, making it stand out more.

We can also adjust the size of the QR code modules using the scale parameter. A scale of 1 means each module is 1 pixel. Let‘s bump up the scale to make a larger QR code:

import segno 

qr = segno.make(‘Enlarge Me‘)
qr.save(‘enlarge.png‘, scale=10)

Enlarged QR Code

Now each module is 10×10 pixels, resulting in a much larger QR code. Increasing the scale can make QR codes easier to scan from a distance.

Finally, we can give our QR codes some style by changing the module colors from the default black and white. Segno‘s save() method accepts dark and light parameters to set the module colors:

import segno

qr = segno.make(‘Colorful QR‘)
qr.save(‘colorful.png‘, dark=‘darkblue‘, light=‘#eeeeff‘)

Colorful QR Code

How stylish! We can use any valid CSS color name or hex code for the dark and light colors. The dark color fills in the actual QR code modules while light is used for the background. With creative color choices, you can make some seriously eye-catching QR codes.

Saving QR Codes in Different Formats

So far we‘ve been saving our QR codes as PNG images, but segno supports several other useful formats. To save as SVG, EPS, or PDF instead of PNG, we simply change the file extension in the save() method:

import segno

qr = segno.make(‘Format Demo‘)

qr.save(‘demo.png‘)
qr.save(‘demo.svg‘)
qr.save(‘demo.eps‘) 
qr.save(‘demo.pdf‘)

This will generate demo.png, demo.svg, demo.eps, and demo.pdf files, each containing our QR code in a different vector or raster format. Having the option to export QR codes as vector graphics (SVG, EPS, PDF) is very handy if you need to print a large QR code or display it on the web, since vector graphics scale to any size without losing quality.

Creating QR Codes for Specific Uses

Now that we‘ve learned the basics of generating and customizing QR codes with segno, let‘s explore some real-world use cases. Segno provides helper functions for creating QR codes with structured data like URLs, geographic coordinates, WiFi access credentials, and more.

URL QR Codes

To share a website link in a QR code, we can simply pass the URL to segno.make():

import segno

url = ‘https://realpython.com/python-qr-code-generator‘
qr = segno.make(url)
qr.save(‘realpython.png‘)

Real Python URL QR Code

When scanned, this QR code will open up the specified URL in the user‘s web browser. This is a great way to link to your website from printed materials or share links with others without requiring them to type in a URL.

WiFi QR Codes

You can generate a QR code containing WiFi access point details, allowing users to scan the code and automatically join the network. Segno provides a helpers.make_wifi() function to construct the WiFi configuration QR code:

from segno import helpers

qr = helpers.make_wifi(ssid=‘MyWiFi‘, password=‘correct-horse-battery-staple‘, security=‘WPA‘)
qr.save(‘wifi.png‘, scale=4)  

WiFi QR Code

Scanning this QR code will prompt the user to join the "MyWiFi" network using the password correct-horse-battery-staple. The security parameter can be ‘WEP‘, ‘WPA‘, or omitted for no encryption. WiFi QR codes are perfect for granting guests easy access to your wireless network at a home, office, or cafe.

Contact QR Codes

We can create a QR code containing contact information in the MeCard format via segno.helpers.make_mecard():

from segno import helpers  

qr = helpers.make_mecard(name=‘John Smith‘, email=‘[email protected]‘, phone=‘+1234567890‘)
qr.save(‘john-smith.png‘, scale=4)

John Smith Contact QR Code

A MeCard QR code includes a person‘s name, email, and phone number. When scanned, the user is prompted to add the contact to their phone‘s address book. Some MeCard fields like email and url accept lists to include multiple values. Contact QR codes make it convenient to share your info or gather contacts at networking events.

Geo URI QR Codes

To encode geographic coordinates in a QR code, we can use segno.helpers.make_geo():

from segno import helpers

qr = helpers.make_geo(lat=40.712, lng=-74.227)  
qr.save(‘location.png‘)

Geographic Coordinates QR Code

Scanning this QR code will open the user‘s maps app and display the specified latitude and longitude. Geo URI QR codes are useful for sharing a location, giving directions, or tagging a particular spot.

Best Practices for QR Code Reliability

To ensure your QR codes are reliably scannable, here are a few tips to keep in mind:

  • Scale the QR code to an appropriate size for the intended scanning distance. A larger scale is better for QR codes that will be scanned from far away.
  • Use a sufficient border size to make the QR code easy to detect. A border of at least 2 modules is recommended.
  • Choose dark and light colors with high contrast. Darker colors for modules and lighter colors for the background tend to work best. Avoid very low contrast designs.
  • Test your QR code with multiple scanning apps to verify it decodes correctly. Fix any issues before deploying the QR code.
  • Include a call-to-action near the QR code to guide users on what to do, e.g. "Scan me for a coupon!"

Following these guidelines will help ensure your QR codes work properly in the real world.

Conclusion

You now have a comprehensive understanding of creating QR codes with Python and the segno library! We‘ve covered generating QR codes, customizing their style, saving in various formats, and making QR codes for specific uses like URLs, WiFi access, contacts, and geo coordinates.

As you‘ve seen, segno makes it straightforward to whip up functional, great-looking QR codes with just a few lines of code. You can use these techniques for sharing information, marketing, customer engagement, and more.

Feel free to experiment further with segno‘s options and helper functions. Consult the official segno documentation to discover even more advanced features. Have fun generating amazing QR codes in your own projects!

Similar Posts