Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating Artistic QR Codes

00:00 Creating artistic QR codes. In the final part of this course, you’ll see how you can use segno to take your QR codes up a notch by replacing the background with an image.

00:11 The image can even be animated when you’re making a code for use online. As you’ll see, this involves an extra step and using the .to_artistic() method. You’ll start out by creating a code with a static image as the background.

00:27 Create a new file called artistic_qrcode.py and open it in your editor or ID. You import segno and make the QR code as normal. But the next line is the file name of the image that you’d like to use as the background of the QR code.

00:45 Here, it’s set to a square version of the Real Python logo that you can download as part of the course materials. The new step is to apply the .to_artistic() method to the QR code object.

00:57 This will allow replacement to the background with the image that you pass to it as you’ll see later. When creating artistic QR codes, you don’t use the .save() method to format the size or colors of the QR code or to set the output file name and location.

01:12 Formatting of artistic QR codes is done by assigning the parameters within .to_artistic(). The background argument is passed the variable that stores the image location.

01:23 Here it’s set to the image variable, and that’s set to the file name of the square Real Python logo. background can also accept pathlib paths, opening up flexibility for automation.

01:35 You set the target argument with the file path and file name of the QR code image to be created.

01:43 If you’d like to change the size of the QR code, you can do so by setting the value of the scale argument in the .to_artistic() method, and here it’s set to five. And that’s it.

01:55 Running the script will create the QR code as you’d expect, and on opening, you’ll see a QR code with the real Python logo in the background.

02:06 While the codes modules look quite different to QR codes you’ve seen already, you should find that it scans in the normal way.

02:15 It’s also possible to use an animated image as the background, so that’s what you’ll be doing next. Create a Python file called animated_qrcode.py.

02:26 Here you’ll create a QR code object with a YouTube link to the song “Smells Like Teen Spirit” by Nirvana. First, `segno’ is imported along with a URL open function from the urllib library.

02:39 This serves as a way of retrieving URLs rather than working with locally stored files, as you saw in the previous example. Next, a QR code object is created to encode the URL of the YouTube video for “Smells Like Teen Spirit” SLTS for short.

02:56 The QR code is stored in the variable slts_qrcode.

03:02 Now the URL for the animated GIF of Kurt Cobain playing the guitar is stored in the `Nirvana URL` variable. You could change this URL to any that point to an animated GIF to create an animated QR code with that image in the background.

03:18 Also, note that it’s the file that contains the animation. It would be possible to use a static image from a URL or use a locally stored animated GIF with a code seen in the previous example.

03:31 urlopen just allows segno to access images stored on the internet.

03:36 The steps here are much the same as the non-animated example you’ve just seen. The .to_artistic() method is applied to the QR code object. This time, the background argument is passed the variable that stores the image’s URL.

03:52 The target argument is set with the file path and file name of the QR code image to be created, and the size of the QR code is set to five.

04:05 Running the script

04:08 and opening the resulting file should result in the animated QR code that you can see on screen with Kurt Cobain playing guitar and a scannable QR code, which will lead you to the video of “Smells Like Teen Spirit” on YouTube.

04:23 In the next section of the course, you’ll take a look back at what you’ve learned.

sftfkatc on May 12, 2024

I downloaded the code zip file from Supporting Material link - I’m looking for the Real Python logo. It could be there and I missed it? Thanks

Chris Bailey RP Team on May 15, 2024

Hi sftkatc, Darren provided me with an updated code zip file, and I’ve replaced the old one. The RP Logo is in a folder named “images”.

Become a Member to join the conversation.