Wednesday, September 13, 2023
HomePythonGenerate Lovely QR Codes With Python – Actual Python

Generate Lovely QR Codes With Python – Actual Python


From restaurant e-menus to airline boarding passes, QR codes have quite a few functions that influence your day-to-day life and enrich the person’s expertise. Wouldn’t or not it’s nice to make them look good, too? With the assistance of this tutorial, you’ll discover ways to use Python to generate lovely QR codes in your private use case.

In its most elementary format, a QR code comprises black squares and dots on a white background, with data that any smartphone or gadget with a devoted QR scanner can decode. In contrast to a conventional bar code, which holds informationation horizontally, a QR code holds the info in two dimensions, and it might probably maintain over 100 instances extra data.

On this tutorial, you’ll discover ways to:

  • Generate a primary black-and-white QR code
  • Change the measurement and margins of the QR code
  • Create colourful QR codes
  • Rotate the QR code
  • Exchange the static background with an animated GIF

Historically, QR codes have been predominantly black-and-white. Now, due to artistic improvements, QR codes are available all kinds of shapes, sizes, and colours. For those who’d wish to discover ways to create not solely two-tone QR codes but additionally colourful and creative ones, then that is the tutorial for you.

Utilizing Python to Generate a Fundamental QR Code

To start with, you’re going to create a black-and-white QR code that encodes some content material. To comply with together with this tutorial, you’ll want to put in Segno, which is a well-liked Python library for producing QR codes. To put in Segno, you possibly can run pip out of your command line:

$ python -m pip set up segno

When you’ve put in segno, create a Python file named basic_qrcode.py. To create a black-and-white QR code object that encodes some content material, you’ll have to make use of the make_qr() perform. This ensures that you just’re making a full-size QR code, and the one obligatory argument you’ll must cross is the data that you just wish to encode.

QR codes are able to dealing with all kinds of knowledge, corresponding to alphanumeric characters, symbols, and even URLs. To start your journey of producing QR codes in Python, you’ll begin by making a QR code object that can encode the textual content "Hey, World":

# basic_qrcode.py

import segno

segno.make_qr("Hey, World")

At this stage, you’ve written the code to encode the textual content "Hey, World" by passing this to the make_qr() perform. It will greet your person with that textual content, and it may additionally provide the choice of looking for "Hey, World" on the Web.

Now, to generate a black-and-white QR code that you may really view and scan, you’ll must retailer the encoded content material as a variable and use the .save() methodology to save lots of your QR code as a picture. Right here’s an instance of how one can create a variable known as qrcode that encodes the textual content "Hey, World" as a black-and-white QR code object, which you then save as a PNG file known as basic_qrcode.png:

# basic_qrcode.py

import segno

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

The .save() methodology serializes the QR code right into a file format of your alternative, so long as the chosen format is supported. Whenever you apply .save() to the variable that you just’ve created with the encoded content material, it is advisable to specify the filename together with an non-obligatory file path. Within the instance above, you’re saving the QR code picture as a file named basic_qrcode.png in the identical listing the place you’ll be executing your code, so that you don’t specify a file path.

For those who’d like to save lots of the picture in a distinct listing, then you possibly can specify the specified file path along with the filename as an argument within the .save() methodology.

When you’ve completed writing the steps for producing a black-and-white QR code in basic_qrcode.py, you’ll must run the Python script out of your command line:

Congratulations, you’ve simply created a black-and-white QR code that encodes the textual content "Hey, World" utilizing make_qr() and .save(). Now you can scan your QR code picture, which you’ll discover in the identical listing that you just’re operating your code from. Or, you possibly can scan the QR code picture beneath:

An image of a basic black and white QR Code

You might have discovered that the QR code picture is a little bit tough to view or learn due to its measurement, so the subsequent merchandise that you just’re going to regulate is the scale of your primary QR code.

Altering the Dimension the QR Code

When attempting to scan your QR code picture or the QR code within the tutorial, you may need discovered it tough to learn due to its measurement.

For those who’d wish to to regulate the scale of the QR code, then you possibly can add a scale parameter to the .save() methodology. The scale parameter is a scaling issue that adjustments the width and top of the QR code picture.

On your reference, the default worth for scale is 1. Whenever you created your first QR code in basic_qrcode.py, the scale of every black or white sq. in your QR code was one pixel extensive and one pixel excessive. Every of those black or white squares is known as a module.

With the code beneath, you possibly can create a QR code that encodes the content material "Hey, World", the place every module is 5×5 pixels in measurement:

# scaled_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "scaled_qrcode.png",
    scale=5,
)

Right here, you’ve created a Python file named scaled_qrcode.py and set scale=5 throughout the .save() methodology. To generate this black-and-white QR code with a scaling issue of 5, you’ll then need to run scaled_qrcode.py from the command line:

$ python scaled_qrcode.py

Nice job! You adjusted the scale of your modules by making use of the .save() methodology to your QR code object with a scale argument of 5. Your QR code ought to appear to be this:

An image of a basic black and white QR Code scaled to 5 x 5 pixels

It could be a little bit tough to see within the picture above, however for those who enlarge it, you then’ll discover that the your QR code picture additionally consists of some clean area across the QR code. Within the subsequent part, you’ll discover ways to alter the scale or take away this clean area.

Formatting the Border of the QR Code

To extend the scannability of the QR code and be sure that gadgets corresponding to smartphones can clearly entry the data, Segno places some clean area across the QR code. This clean area is known as the quiet zone. You even have the choice of modifying the scale of this quiet zone by making adjustments to the border parameter throughout the .save() methodology.

You set the scale of the margin across the QR code by specifying an integer worth to the border argument of the .save() methodology. By default, the scale of the quiet zone is 4 modules on both sides.

If you wish to utterly take away the quiet zone, you are able to do so by setting border=0 within the .save() methodology. Within the code beneath, you take away the border out of your QR code:

# borderless_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "borderless_qrcode.png",
    scale=5,
    border=0,
)

You’ve created a Python script named borderless_qrcode.py and set the worth of the border argument within the .save() methodology to 0. It will create a picture with none clean areas across the QR code.

To generate a QR code that encodes the textual content "Hey, World" with out a border and with a scaling issue of 5, run borderless_qrcode.py out of your command line:

$ python borderless_qrcode.py

Identical to that, you’ve eliminated the quiet zone in your QR code. Give it a whirl by scanning your QR code picture. It’s also possible to scan the QR code picture beneath:

This is an image of a QR Code without a border

If you wish to improve the scale of the quiet zone and create a wider border in your QR code, you are able to do so by rising the worth of border. For instance, to create a QR code with a border of ten white modules, you possibly can set border=10 within the .save() methodology:

# wide_border_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "wide_border_qrcode.png",
    scale=5,
    border=10,
)

Your Python script named wide_border_qrcode.py, during which you set the worth of the border argument to 10 within the .save() methodology, will create a QR code with a margin of ten mild modules.

To generate a 5×5 pixel QR code that encodes the textual content "Hey, World" with a wider border, run wide_border_qrcode.py out of your command line:

$ python wide_border_qrcode.py

You’ve simply created a PNG file with a QR code of measurement 5×5 pixels and a border of ten mild modules. Test it out:

This is an image of a QR Code with a wide border of 10 light modules

For the reason that QR code has a white background, and the colour of the quiet zone is white, you may need to enlarge the picture to view the broader margin across the QR code. Ideally, you need to be capable to change the colour of the background or quiet zone. Within the subsequent part, you’ll discover ways to create extra colourful QR codes with Python. You’ll then be capable to change the colour of the background, the quiet zone, and the info modules.

Altering the Colours of the QR Code

For those who’d wish to create QR codes together with your favourite colours, then the .save() methodology has non-obligatory parameters that you may add to make your codes extra colourful. On this part, you’ll modify the colour of the background, the quiet zone, and the sunshine and darkish modules of your QR codes. All you need to do is specify the key phrase and a colour of alternative. You should use the RGB format, the title of the colour, or a hexadecimal worth (#RRGGBB).

For instance, if you wish to change the colour of the background to mild blue, then you possibly can add mild="lightblue" throughout the .save() methodology:

# lightblue_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "lightblue_qrcode.png",
    scale=5,
    mild="lightblue",
)

You’ve created a Python script named lightblue_qrcode.py and set the worth of lightblue to the mild argument throughout the .save() methodology. This code will exchange the white background with a light-weight blue background.

On your reference, you can too exchange the colour title with the RGB or hexadecimal format. If you wish to set the background to mild blue in RGB format, then you possibly can exchange mild="lightblue" with mild=(173, 216, 230). To make use of a hexadecimal worth, you possibly can exchange mild="lightblue" with mild="#ADD8E6".

With that code in place, you possibly can generate a QR code with a background colour of sunshine blue by operating lightblue_qrcode.py out of your command line:

$ python lightblue_qrcode.py

Nice! Now you may have a stunning mild blue background, and the quiet zone is extra seen. To reap the benefits of your handiwork, scan your QR code picture or the QR code picture beneath:

This is an image of a QR Code with a light blue background

It’s also possible to change the colours of all of the black modules of the QR code. So as to take action, you possibly can set a price with a colour of your alternative within the darkish parameter within the .save() methodology. For instance, the code beneath will change the colours of the darkish modules to darkish blue:

# darkblue_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "darkblue_qrcode.png",
    scale=5,
    darkish="darkblue",
)

Your script will set the worth of all of the black modules to darkish blue. To generate this QR code, run darkblue_qrcode.py out of your command line:

$ python darkblue_qrcode.py

On this iteration, your QR code encoding the textual content "Hey, World" has darkish blue modules instead of the normal black ones:

This is an image of a QR Code with dark blue data modules

This time, the quiet zone isn’t as seen for the reason that background colour is white. In actual fact, you will have to enlarge the picture to see the margins. You may at all times change the colour of the background by specifying a colour of your alternative within the mild argument throughout the .save() methodology.

One other adjustment that you can make to your QR code picture is altering the colour of the quiet zone by including a quiet_zone parameter within the .save() methodology. Right here’s an instance of an addition you can make to darkblue_qrcode.py to vary the colour of the area across the QR code from white to maroon:

# darkblue_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "darkblue_qrcode.png",
    scale=5,
    darkish="darkblue",
    quiet_zone="maroon",
)

For those who run python darkblue_qrcode.py out of your command line once more, you then’ll create a QR code with maroon borders:

This is a QR Code with dark blue dark modules and a maroon quiet zone

Up to now, you’ve used the mild and darkish parameters to vary the colour of all of the white and black modules of the QR code. Nevertheless, not the entire modules within the QR code home the info that’s encoded. If you wish to take your QR codes up a stage, then you can too change the colour of the knowledge modules. The information modules are the black and white blocks the place the info is definitely saved.

To alter the colours of the darkish knowledge modules of the QR code, you possibly can set a colour of your alternative for the data_dark argument throughout the .save() methodology. For instance, if you wish to change the colour of the darkish knowledge modules to inexperienced, then you possibly can create a Python script known as green_datamodules_qrcode.py and add data_dark=inexperienced throughout the .save() methodology:

# green_datadark_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "green_datadark_qrcode.png",
    scale=5,
    mild="lightblue",
    darkish="darkblue",
    data_dark="inexperienced",
)

By including the argument data_dark within the .save() methodology and set it to inexperienced, you alter the colours of the darkish knowledge modules to inexperienced. It’s also possible to change the colour of all of the darkish modules that don’t include knowledge to darkish blue. To do that, you add darkish="darkblue". When you’re at it, you go forward and set the background to mild blue by including mild="lightblue" throughout the .save() methodology.

To generate a QR code with inexperienced darkish knowledge modules, run green_datadark_qrcode.py out of your command line:

$ python green_datadark_qrcode.py

Cool, now you possibly can inform which modules encode knowledge! They stand out in a vibrant inexperienced, as you possibly can see beneath:

This is an image of a QR Code with green dark data modules on a light blue background

It’s also possible to add a data_light argument with a colour of your option to the .save() methodology to vary the colours of the white knowledge modules. To alter the colours of the sunshine knowledge modules to mild inexperienced, you possibly can add data_light="lightgreen":

# green_datamodules_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode.save(
    "green_datamodules_qrcode.png",
    scale=5,
    mild="lightblue",
    darkish="darkblue",
    data_dark="inexperienced",
    data_light="lightgreen",
)

For those who add the argument data_light within the .save() methodology and set it to lightgreen, this can change the colours of the sunshine knowledge modules to mild inexperienced. For those who run python green_datamodules_qrcode.py out of your command line once more, you then’ll create a QR code with darkish blue modules and inexperienced and light-weight inexperienced knowledge modules on a light-weight blue background:

This is an image of a QR Code with green dark data modules and light green light data modules on a light blue background

Now you can scan your colourful "Hey, World" QR code, or you possibly can scan the QR code picture above.

On this part, you’ve realized use the the .save() methodology to vary the colours of the sunshine and darkish elements of the QR codes. Have enjoyable attempting totally different combos of colours, and you can too seek advice from the Segno documentation for an entire listing of parameters that you may tweak to create colourful QR codes.

Altering the colours of the background, quiet zone, and modules of your QR code is fairly cool, however you can additionally rotate your QR codes for a singular artistic twist. Within the subsequent a part of the tutorial, you’ll discover ways to apply the .to_pil() methodology to rotate your QR codes.

Rotating the QR Code

With Segno, you’re additionally in a position to manipulate your QR code by rotating it or including an animated background. For those who’d wish to create QR codes with superior graphical operations, you then’ll want to put in the dependencies qrcode-artistic and pillow:

$ python -m pip set up pillow qrcode-artistic

With these dependencies, you’ll be capable to use two further strategies, .to_pil() and .to_artistic(), when creating your QR code with make_qr(). It will convert your QR code right into a Pillow Picture occasion, which you’ll then use to rotate the QR code or exchange the static background with an animated occasion.

For those who’d wish to rotate your QR code, you then’ll use the .to_pil() methodology and specify the rotation angle in levels. Beneath, you encode your trusty "Hey, World" textual content, however you additionally rotate the QR code object by 45 levels:

# rotated_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")

qrcode_rotated = qrcode.to_pil().rotate(45)
qrcode_rotated.save("rotated_qrcode.png")

You’ll need to specify the diploma of rotation throughout the .to_pil().rotate() methodology. The course of rotation is counterclockwise. By utilizing the .to_pil() methodology and specifying the diploma of rotation within the .rotate() methodology, you’ve written the code to create a black-and-white QR code object rotated by 45 levels. Lastly, you utilized the .save() methodology to your qrcode_rotated variable.

Now it’s time to run rotated_qrcode.py out of your command line and see what you get:

$ python rotated_qrcode.py

It’s a little bit small because you haven’t alter the scale (but), however now you can scan your rotated QR code picture, or you possibly can scan the QR code picture beneath:

This is an image of a truncated rotated QR Code

You might have noticed that the QR code is truncated. Specifically, the corners of the quiet zone have been lower off. You might have to enlarge the picture to note this. For those who’d wish to hold the entire picture throughout rotation, then you possibly can add an broaden argument within the .to_pil().rotate() methodology and set it to True. The code beneath will rotate the QR code by forty-five levels and broaden the canvas to include the entire code:

# rotated_qrcode.py

import segno

qrcode = segno.make("Hey, World")

qrcode_rotated = qrcode.to_pil().rotate(45, broaden=True)
qrcode_rotated.save("rotated_qrcode.png")

Right here, you’ve specified the diploma of orientation and set broaden=True within the to_pil().rotate() methodology to create a black-and-white QR code object rotated by 45 levels and enlarged. You then apply the .save() methodology to your qrcode_rotated variable and cross the filename with extension.

To generate this QR code, run python rotated_qrcode.py out of your command line once more:

This is an image of an expanded rotated QR Code

It’s possible you’ll wish to alter the scale of the rotated QR code as you probably did in scaled_qrcode.py, otherwise you would possibly wish to change the background colour such as you did in lightblue_qrcode.py. Nevertheless, if you wish to format a rotated QR code, including the scale or mild argument to the .save() methodology gained’t change the scale or background colour of the QR code. Whereas it seems to be like earlier than, you’re really calling .save() on a PIL object and never a QR code object.

Because you’re utilizing the .to_pil() methodology to rotate your QR code, you’ll have so as to add the parameters that resize and alter the colours of the QR code inside .to_pil() as an alternative. Beneath, you add a scale issue of 5 to the QR code object and alter the colour of the darkish elements of the QR code to inexperienced and the sunshine elements to mild blue:

# formatted_rotated_qrcode.py

import segno

qrcode = segno.make_qr("Hey, World")
qrcode_rotated = qrcode.to_pil(
    scale=5,
    mild="lightblue",
    darkish="inexperienced",
).rotate(45, broaden=True)
qrcode_rotated.save("formatted_rotated_qrcode.png")

To alter the scale of the QR code, you set the worth within the scale argument. You alter the mild and darkish arguments within the .to_pil() methodology to vary the colour of the sunshine and darkish sections of the QR code.

You might have seen that the rotated QR code has some further clean area that’s not the identical because the quiet zone across the QR code. This extra background is created to overlay the rotated QR code. To change the colours of the darkish modules and the extra border, which can be black by default, you possibly can alter the worth of the darkish argument within the .to_pil() methodology with a colour of your alternative.

To generate a 5×5 pixel QR code with a light-weight blue background, plus inexperienced darkish modules and the extra background, run formatted_rotated_qrcode.py out of your command line:

$ python formatted_rotated_qrcode.py

You may scan your formatted and rotated QR code, or you possibly can scan the picture beneath:

This is an image of an expanded rotated QR Code of 5 x 5 pixels with a light blue background and the dark parts are green.

Not solely can you alter the background colour of the QR code, however you can too exchange it with a shifting picture. Within the last part of this tutorial, you’ll use the .to_artistic() methodology to interchange a static background with an animated one.

Creating Animated QR Codes

Within the last part of this tutorial, you’re going to create probably the most lovely QR code by changing the static background with an animated picture, corresponding to a GIF. This requires a number of further steps, however the outcomes are value it.

For those who’d wish to create a QR code with an animated picture because the background, then the very first thing it is advisable to do is create a Python file known as animated_qrcode.py. Within the instance beneath, you create a QR code object with a YouTube hyperlink to the tune “Smells like Teen Spirit” by Nirvana:

# animated_qrcode.py

import segno

slts_qrcode = segno.make_qr("https://www.youtube.com/watch?v=hTWKbfoikeg")

You’ve simply written the code to create a QR code object with the make_qr() perform. This QR code object encodes the YouTube hyperlink for the basic “Smells like Teen Spirit,” or SLTS for brief. You retailer this QR code because the variable slts_qrcode.

The second step that you just’ll have to hold out is to specify the file path of the animated picture. For those who’d like to make use of an animated picture from a URL, you then’ll must import and use the urlopen() perform from the urllib library. In any other case, you possibly can skip this step:

# animated_qrcode.py

import segno
from urllib.request import urlopen

slts_qrcode = segno.make_qr("https://www.youtube.com/watch?v=hTWKbfoikeg")
nirvana_url = urlopen("https://media.giphy.com/media/LpwBqCorPvZC0/giphy.gif")

The urlopen() perform serves as a approach of retrieving URLs, and on this situation, you’re passing the hyperlink to a GIF that includes Kurt Cobain passionately taking part in the guitar. You retailer this as a variable known as nirvana_url. You may skip this step for those who use an animated picture saved domestically, which suggests you’re not fetching a shifting picture from a URL.

The ultimate step is to use the .to_artistic() methodology to your QR code object. It will exchange the static background with a shifting background. Beneath, you write the code generate a QR code with modules of 5×5 pixels, with the animated picture of Kurt Cobain headbanging because the background:

# animated_qrcode.py

import segno
from urllib.request import urlopen

slts_qrcode = segno.make_qr("https://www.youtube.com/watch?v=hTWKbfoikeg")
nirvana_url = urlopen("https://media.giphy.com/media/LpwBqCorPvZC0/giphy.gif")
slts_qrcode.to_artistic(
    background=nirvana_url,
    goal="animated_qrcode.gif",
    scale=5,
)

Within the .to_artistic() methodology, you’ve added a background argument with both the file path of your animated picture or the variable that shops the picture’s URL. You’ve additionally set the goal argument with the file path and filename of the QR code picture. For those who’d like to vary the scale of the QR code, you are able to do so by setting the worth of the scale argument within the .to_artistic() methodology.

When creating creative QR codes, such because the rotated QR code in formatted_rotated_qrcode.py and the QR code that you just’re about to generate with an animated background, you don’t use the .save() methodology to format the scale or colours of the QR code. For those who’d wish to format the creative QR code, then you possibly can assign the parameters inside .to_artistic().

To create a GIF file known as animated_qrcode.gif in the identical location the place you’re executing your code, you run animated_qrcode.py out of your command line:

$ python formatted_rotated_qrcode.py

By making use of the .to_artistic() methodology to your QR code object and setting the worth of background to the URL of an animated picture, you’ve created a QR code that exhibits Kurt Cobain moshing out:

This contains a QR Code with a moving image of Kurt Kobain head banging

For those who scan the picture above or your native QR code picture, then this can lead you to the YouTube web page for the “Smells like Teen Spirit” music video by Nirvana.

Conclusion

Now that you’ve an understanding of generate QR codes in Python utilizing the library Segno, you possibly can develop your personal lovely QR Codes. You can also make something from conventional black-and-white QR codes to creative QR codes with rotated and animated backgrounds.

On this tutorial, you’ve realized :

  • Use make_qr() and .save() to create black-and-white QR codes
  • Alter the module measurement and quiet zone of your QR code
  • Personalize the darkish and light-weight modules together with your favourite colours
  • Use the .to_pil().rotate() methodology to rotate your QR codes
  • Create QR codes with animated backgrounds utilizing .to_artistic()

Subsequent time you take a look at a black-and-white QR code, you possibly can consider the numerous ways in which you should use Segno to generate artistic QR codes to seize your person’s consideration. Have enjoyable producing your personal lovely QR codes with Python.

RELATED ARTICLES

Most Popular

Recent Comments