Learn how to create stunning QR codes with the Segno Python module. This article will teach you everything you need to know, from generating QR codes to customizing their appearance.
A quick response (QR) code is a barcode that a digital device can easily scan. It encodes data as a series of pixels in a square grid.
Tracking information about supply chains using QR codes is very useful in marketing and advertising campaigns.
The International Organization for Standardization certified QR codes as a global standard in 2000. They are an improvement over the previous uni-dimensional barcodes (ISO).
QR codes were developed in the 1990s to provide more information than a regular barcode. They were created by Denso Wave, a Toyota affiliate, to monitor the production of vehicles.
In contrast to barcodes, which need a beam of light to bounce off the parallel lines, QR codes may be scanned digitally by devices like smartphones.
QR codes are used in cryptocurrency systems to enable digital payments, such as when displaying a Bitcoin address. QR codes are also often used to communicate site URLs to mobile devices.
In this article, we will use the segno
library to create beautiful QR codes that perform so many functions.
Segno is an open-source QR code generator that lets you create both regular and micro QR codes with very little effort. It also doesn't have any dependencies.
Segno offers a few serialization types like SVG, EPS, PNG, PDF, and text output. None of these serializers calls on an external library. Through a plugin design, Segno offers other serialization types. PyPy and Python versions 2.6 to 3.4 were used for testing.
Just like every other Python library, you can install Segno via pip.
pip install segno
So, using the .make()
method, let's start by making the most basic QR code possible. Since the content is so brief, Segno automatically generates a fun-sized "micro QR" code, which carries raw data and that you can copy or transfer.
import segno
price_tag = segno.make("Hello World")
price_tag.save("hello-world.png")
The QR code is generated and saved into our project directory.
hello-world.png
We can add a border to the QR code to make it look more attractive. You can do this by adding the border
parameter to the .save()
method.
import segno
qrcode = segno.make('Vampire Blues')
qrcode.save('vampire-blues.png', border=5)
output: vapire-blues.png
The QR codes that we have been creating have been very small. We can make them look bigger by adding the scale parameter like this:
import segno
qrcode = segno.make_qr('Welcome')
qrcode.save('welcome.png', scale=10)
welcome.png
We can also create some colorful QR codes with Segno – they are really beautiful. This is possible with the help of many serializers which accept the parameters dark and light to specify the color of the dark modules and light modules.
Here are a couple examples to give you an idea of what's possible:
import segno
qrcode = segno.make("Green ave, Kingston")
qrcode.save('address.png', dark='darkred', light='lightblue', scale=10)
address.png
import segno
qrcode = segno.make("Green ave, Kingston")
# Dark modules with alpha transparency
qrcode.save('address2.png', dark='#0000ffcc', scale=10)
address2.png
import segno
micro_qrcode = segno.make('Rain', error='q')
micro_qrcode.save('rain.png', dark='darkblue', data_dark='steelblue', scale=5)
rain.png
Segno give us the flexibility to save our generated QR codes in different file formats such as .svg
, .png
, .eps
, and .pdf
.
Here's how you would do that:
import segno
qrcode = segno.make('Beatles')
qrcode.save('Beatles.svg')
qrcode.save('Beatles.png')
qrcode.save('Beatles.eps')
We can easily generate a QR code that links to a URL. This lets us get online content by using the same technique with a little bigger payload.
We will create a QR code that links to my YouTube channel (Velcast Podcast), and then we will save it.
Here's the code for that:
import segno
video = segno.make('https://www.youtube.com/channel/UCNhFxpk6hGt5uMCKXq0Jl8A')
video.save('Video.png', dark="yellow", light="#323524", scale=5)
And the result:
video.png
We can also use the Segmo library to create a QR code for wifi configuration. The segno.helpers
module offers factory methods to generate standard QR codes for encoding geographic coordinates, vCards
and MeCards
, WIFI setups
, and EPC QR Codes
.
The error correction level "L" is utilized in creating QR codes. If possible, we will apply the higher error correction level without altering the QR Code version.
The density of the QR code picture decreases with decreasing error correction level, which enhances minimum printing size. The more damage it can withstand before losing its ability to be read, the greater the error correction level.
The optimal balance between density and toughness for general marketing use is Level L or Level M. In industrial settings where maintaining a clean or undamaged QR code may be difficult, Level Q and Level H are the best options.
from segno import helpers
qrcode = helpers.make_wifi(ssid='MyWifi', password='1234567890', security='WPA')
qrcode.designator
'3-M'
qrcode.save('wifi-access.png', scale=10)
wifi-access.png
We can also do this code this way:
import segno
wifi_settings = {
ssid='(Wifi Name)',
password='(Wifi Password)',
security='WPA',
}
wifi = segno.helpers.make_wifi(**wifi_settings)
wifi.save("Wifi.png", dark="yellow", light="#323524", scale=8)
We can use either of the two options for the code. They result in the same thing but represent different styles of writing and presentation.
Common use cases of using QR codes for wifi access include:
We can also store contact details in a QR code. We just need to make use of the helpers.make_mecard()
method and we can pass in the contact details. It's also important to note that we can pass in a list to the method.
Let's look at an example:
from segno import helpers
qrcode = helpers.make_mecard(name='Shittu Olumide', email='me@example.com', phone='+123456789')
qrcode.designator
'3-L'
# Some params accept multiple values, like email, phone, url
qrcode = helpers.make_mecard(name='Shittu Olumide',
email=('me@example.com', 'email@example.com'),
url=['http://www.example.com', 'https://example.come/~olu'])
qrcode.save('mycontact.png', scale=5)
Segno also allows you to perform the following actions:
Now that you've learned how to create QR codes, here are some of their applications in businesses and in our daily lives:
And many more.
Hopefully, this short article has whetted your appetite and inspires you to employ QR codes in your work and personal projects.
By developing some eye-catching, functioning QR codes in this article, we tested the Segno Python module. You can read the official documentation to learn more about the library.
Source: https://www.freecodecamp.org
#python #qrcode