Generate Beautiful QR Codes With Python

From restaurant e-menus to airline boarding passes, QR codes have numerous applications that impact your day-to-day life and enrich the user’s experience. Wouldn’t it be great to make them look good, too? With the help of this tutorial, you’ll learn how to use Python to generate beautiful QR codes for your personal use case.

In its most basic format, a QR code contains black squares and dots on a white background, with information that any smartphone or device with a dedicated QR scanner can decode. Unlike a traditional bar code, which holds informationation horizontally, a QR code holds the data in two dimensions, and it can hold over a hundred times more information.

In this tutorial, you’ll learn how to:

  • Generate a basic black-and-white QR code
  • Change the size and margins of the QR code
  • Create colorful QR codes
  • Rotate the QR code
  • Replace the static background with an animated GIF

Traditionally, QR codes have been predominantly black-and-white. Now, thanks to creative innovations, QR codes come in all sorts of shapes, sizes, and colors. If you’d like to learn how to create not only two-tone QR codes but also colorful and artistic ones, then this is the tutorial for you.

Using Python to Generate a Basic QR Code

To begin with, you’re going to create a black-and-white QR code that encodes some content. To follow along with this tutorial, you’ll need to install Segno, which is a popular Python library for generating QR codes. To install Segno, you can run pip from your command line:

$ python -m pip install segno

Once you’ve installed segno, create a Python file named basic_qrcode.py. To create a black-and-white QR code object that encodes some content, you’ll have to use the make_qr() function. This ensures that you’re creating a full-size QR code, and the only mandatory argument you’ll need to pass is the information that you want to encode.

QR codes are capable of handling all types of data, such as alphanumeric characters, symbols, and even URLs. To begin your journey of generating QR codes in Python, you’ll start by creating a QR code object that will encode the text "Hello, World":

# basic_qrcode.py

import segno

segno.make_qr("Hello, World")

At this stage, you’ve written the code to encode the text "Hello, World" by passing this to the make_qr() function. This will greet your user with that text, and it might also offer the option of searching for "Hello, World" on the Internet.

Now, to generate a black-and-white QR code that you can actually view and scan, you’ll need to store the encoded content as a variable and use the .save() method to save your QR code as an image. Here’s an example of how you can create a variable called qrcode that encodes the text "Hello, World" as a black-and-white QR code object, which you then save as a PNG file called basic_qrcode.png:

# basic_qrcode.py

import segno

qrcode = segno.make_qr("Hello, World")
qrcode.save("basic_qrcode.png")

The .save() method serializes the QR code into a file format of your choice, as long as the chosen format is supported. When you apply .save() to the variable that you’ve created with the encoded content, you need to specify the filename including an optional file path. In the example above, you’re saving the QR code image as a file named basic_qrcode.png in the same directory where you’ll be executing your code, so you don’t specify a file path.

If you’d like to save the image in a different directory, then you can specify the desired file path together with the filename as an argument in the .save() method.

Once you’ve finished writing the steps for generating a black-and-white QR code in basic_qrcode.py, you’ll need to run the Python script from your command line:

$ python basic_qrcode.py

Congratulations, you’ve just created a black-and-white QR code that encodes the text "Hello, World" using make_qr() and .save(). You can now scan your QR code image, which you’ll find in the same directory that you’re running your code from. Or, you can scan the QR code image below:

An image of a basic black and white QR Code

You may have found that the QR code image is a little difficult to view or read because of its size, so the next item that you’re going to adjust is the size of your basic QR code.

Changing the Size the QR Code

When trying to scan your QR code image or the QR code in the tutorial, you might have found it difficult to read because of its size.

Read the full article at https://realpython.com/python-generate-qr-code/ »


[ Improve Your Python With ? Python Tricks ? – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

\"IT電腦補習
立刻註冊及報名電腦補習課程吧!

Find A Teacher Form:
https://docs.google.com/forms/d/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ/viewform?edit_requested=true#responses

Email:
public1989two@gmail.com






www.itsec.hk
www.itsec.vip
www.itseceu.uk

Be the first to comment

Leave a Reply

Your email address will not be published.


*