Generate Attractive QR Codes Using Python

In this tutorial, we will learn how to generate the attractive QR code using the Python code. We will also discuss changing the size of the QR code, formatting the Border of the QR code, rotating, creating animated QR codes.

As we can see in our day to day life, QR codes have many uses, and they can also look attractive. In this tutorial, you will discover how to use Python to create visually appealing QR codes for your specific needs.

In its simplest form, a QR code consists of black squares and dots on a white background. It contains data that can be decoded by any smartphone or device with a QR scanner. Unlike a regular barcode that stores information in a horizontal line, a QR code stores data in two dimensions, allowing it to hold more than a hundred times the amount of information.

In the past, QR codes were mainly black and white. However, due to creative advancements, QR codes now come in various shapes, sizes, and colors. If you're interested in discovering how to design QR codes that are not limited to just two colors but can be colorful and artistic, then this tutorial is what you're looking for.

Generate Basic QR code using Python

To generate the Basic QR code, we need to install segno library. We can install it using the following command.

Once the library is installed, we will create a Python file and write the code to generate the code.

Example -

Output:

Generate Attractive QR Codes Using Python

In the above code snippet, we've written the code to encode the message "Hello, World" by passing it as an input to the make_qr() function. This action will display a welcoming message to your user, and it might also provide them with the choice to initiate an online search for "Hello, World."

To generate a black-and-white QR code that is both viewable and scannable, we should first encode the content and store it as a variable. After that, we can use the .save() method to save the QR code as an image. Here's an illustration of how you can establish a variable named qrcode to encode the message "Hello, World" into a black-and-white QR code object. Subsequently, you save this QR code as a PNG file, naming it simple_qr.png.

The `.save()` method performs the task of converting the QR code into a file format of your choosing, provided that the selected format is supported. When using `.save()` on the variable containing the encoded content, it's essential to indicate the filename, which may include an optional file path. In the previous example, the QR code image is saved as "simple_qrcode.png" in the same directory where you run your code, eliminating the need to specify a separate file path.

When we complete the step, it will generate a black and white QR code that you can see in the saved file.

Changing the Size the QR Code

In this section, we will learn to change the size of the QR code. To modify the QR code's dimensions, you can include a scale parameter when using the .save() method. This scale parameter acts as a scaling factor, allowing you to adjust the width and height of the QR code image. We take the previous QR code as a reference. Let's understand the following example.

Example -

Output:

Generate Attractive QR Codes Using Python

As we can see that, the size is adjusted your modules by applying the .save() method to your QR code object with a scale argument of 5.

Formatting the Border of the QR Code

To enhance the ease with which QR codes can be scanned, ensuring that devices like smartphones can easily interpret the information; Segno incorporates a margin of empty space around the QR code, known as the "quiet zone." You can also adjust the size of this quiet zone by making alterations to the "border" parameter within the .save () method.

You can control the margin size surrounding the QR code by providing an integer value to the border parameter in the save() method. The default quiet zone size encompasses four modules on each side. You can also remove the quiet zone entirely, you can achieve this by setting "border=0" in the `.save()` method. In the following code, you remove the border from your QR code. Let's understand the following example.

Example -

Output:

Explanation -

In the above code, we named the wide_border_qrcode.py and set the border value of the border argument to 10 in the save() method.

Because both the QR code and the quiet zone have white backgrounds, you may need to zoom in to notice the wider margin surrounding the QR code. Ideally, you should be able to customize the color of the background or the quiet zone. In the next section, you'll discover how to generate more vibrant and colorful QR codes using Python. This will allow you to modify the background color, the quiet zone color, and the data module colors as well.

Changing the Color of the QR Code

You can create QR codes with your preferred colors, you can utilize optional parameters within the .save() method to add more colors to your codes. In this section, you'll be able to customize the background color, the quiet zone color, as well as the light and dark modules of your QR codes. All that's required is specifying the appropriate keyword and your chosen color. You have the flexibility to use the RGB format, the color's name, or even a hexadecimal value (#RRGGBB).

For instance, if you want to alter the background color to light blue, you can include light='lightblue' within the .save() method.

Example -

Output:

Generate Attractive QR Codes Using Python

In the above Python code, we named "colored_qrcode.py," we've configured the "light" parameter within the .save() method to attain a light blue background. This adjustment replaces the default white background with a soothing light blue one.

You have the flexibility to substitute the color name with either the RGB format or a hexadecimal value. For instance, if you prefer to set the background to light blue using RGB format, you can replace "light='lightblue'" with "light=(173, 216, 230)." Alternatively, you can use a hexadecimal value like "light='#ADD8E6'" in place of "light='lightblue'."

You also have the option to customize the colors of all the black modules within the QR code. To achieve this, simply assign your preferred color value to the "dark" parameter within the .save() method. Let's understand the following example -

Example -

In this case, the quiet zone might not be as conspicuous because the background color is white. In fact, you might need to zoom in on the image to clearly see the margins. You can modify the background color by specifying your preferred color in the "light" parameter within the .save() method.

You can further customize your QR code image by altering the color of the quiet zone. To do this, you can introduce a quiet_zone parameter within the .save() method. Here's an example of how you can enhance "darkblue_qrcode.py" to switch the color of the area surrounding the QR code from white to maroon.

Example -

Output:

Generate Attractive QR Codes Using Python

To modify the colors of the dark data modules within the QR code, you can provide your preferred color using the "data_dark" parameter within the .save() method. For instance, if you want to transform the color of the dark data modules into green, you can create a Python script named "green_datamodules_qrcode.py" and insert "data_dark=green" within the .save() method.

Rotating the QR Code

The segno module allows you to change the orientation of the QR to a different angle, making it versatile for various use cases. Rotating a QR code can be helpful for scenarios where the code needs to be scanned from an unconventional angle or orientation.

To generate QR codes with more intricate graphical enhancements, you'll have to install the required dependencies, specifically qrcode-artistic and pillow.

After installing these library, we can use the two advance methods - .to_pill() and .to_artistic() and to make the QR code with make_qr(). This will convert QR code into a Pillow Image.

Example -

Output:

Generate Attractive QR Codes Using Python

Explanation -

To do rotation, you'll need to indicate the rotation angle within the .to_pil().rotate() method. The point to remember is that the rotation direction is counterclockwise. Utilizing the .to_pil() method and specifying the rotation angle within the .rotate() method, you have write the code to generate a QR code in black and white, rotated by 45 degrees. Lastly, you've used the .save() method on your qrcode_rotated variable.

Create Animated QR Codes

In this section, we will learn how to create an animated QR codes by replacing the static background with an animated image, such as GIF. To generate a QR code with an animated image as the background, start by creating a Python file named animated_qrcode.py. In the following example, we create a QR code object containing a YouTube link to the song "Baby" by Justine Bieber.

Example -

In the above code, we have create a QR code object using the make_qr() function. When this QR code is scanned by the user, it will show the YouTube link of song.

To make it animated, we will use the urllib library and its function urlopen(). Let's understand the following example.

Example -

The last step to take is to use the `.to_artistic()` method on your QR code object. This operation will substitute the fixed background with a dynamic one. Below, you script the procedure to produce a QR code with 5x5 pixel modules.

Conclusion

In this tutorial, we've explored how to create attractive and functional QR codes using Python. QR codes have become important part in our daily lives, and making them visually appealing can enhance their user experience.

We started with the basics, encoding messages, and generating black-and-white QR codes. We learned how to adjust their size, control the quiet zone, and even rotate them using the Segno library.

Next, we explored creating colorful QR codes, letting us personalize backgrounds, quiet zones, and module colors. We discussed about using color names, RGB formats, and hexadecimal values to get the look we want.






Latest Courses