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

Comparing Other Options

00:00 Other options in Segno. So far, you’ve been creating only QR codes with a small amount of data in them, the text of “Hello World”. But QR codes can hold a much larger amount of data and the format of the code will change depending on that data.

00:16 The smallest code is called a MicroQR code. If left to its own devices, Segno will use the smallest possible code for the data which is being stored in it.

00:26 You may remember back at the start of the course that there was a method mentioned called make(). Rather than make_qr(), which you’ve been using throughout.

00:34 You’ll now compare both methods back to back. Often it’s useful to use the Python REPL when investigating a new library or package. So for this, that’s what you’ll do.

00:44 Fire up a Python REPL by typing Python at the command prompt. Or you may want to use an extended REPL such as B Python, which you’ll see here as it offers command highlighting and color coding.

00:56 Let’s take a look at what happens when you use make() instead of make_qr() with the text “Hello, world”. First, Segno is imported as usual and then two codes containing the same text are created.

01:09 One uses the make_qr() function you’ve seen many times before, and the other uses make(). make() will create the smallest possible code, which in this case is a MicroQR.

01:25 Both are saved to files to allow inspection

01:29 and you can see them on screen.

01:34 There are other methods available for code objects aside from save(). One is symbol_size(), which returns the size and pixel of the code image.

01:44 You can see here that the MicroQR is smaller than the regular QR. You can also inspect the version attribute of the code with the QR code being version one and the microcode being version M four.

01:58 You’ll see more about versions a little later.

02:02 Depending on the reader that you are using, you may find that microQR codes are harder to read or even unreadable, but it can be useful to know that they’re available for you if you do need to use them.

02:14 One area where QR codes can be particularly useful is storing wifi network connection information. This can avoid difficulty in entering the right settings when accessing a network on any device which has a camera.

02:26 It is possible to create such a code manually by entering the appropriate text into the code. An example of a wifi network configuration is seen on screen, but Segno can create this in a simpler way using the helpers odule. First, Segno’s helper module is imported.

02:45 If you are only using a helper and not creating a QR code directly with Segno, then you’ll only need to import this and not Segno itself. helpers.make_wifi() is then used with the appropriate arguments for the network and you’ll need to change these to suit yours

03:05 and then the code is saved in the normal way.

03:14 The code created by this script is seen on screen, and if you scan it, you should be prompted as to whether or not you want to join the “My Network” network.

03:24 Throughout this course, you’ve just been using the text “Hello World” in the QR codes. This has kept the Python code short and meant that you’ve had small images to deal with, but QR codes can encode a lot more data.

03:38 As you increase the amount of information in a QR code, the code will become larger to allow this. The code seen on screen generates eight versions of QR code by creating random strings of increasing lengths, which equate to different versions and therefore sizes of QR code.

04:24 You can see the eight codes on screen scaled to be the same size overall, even though the pixel sizes will change. Additional structures appear in the codes as they increase in size.

04:36 These are needed to allow reliable scanning of the code to accommodate any distortion present in the code, the viewing angle or the camera itself. On screen, you can see that the data areas of the codes have been set to gray to highlight these structures.

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

05:01 While it is possible to specify a path as a string, Windows uses a different file system structure and divider to indicate directories. So what works on Windows will break on macOS, and Linux and vice versa.

05:13 So the example seen on screen uses pathlib to avoid this problem.

05:29 This line stores the current working directory. This creates a path to the new location for the QR code inside the images directory. This creates the images directory, which is the parent of the file. exists_ok=True allows the code to run even if the directory already exists.

05:49 Finally, the QR code is saved as you saw before, but this time p is passed as the path to the image. Segno can accept pathlib paths, so this can make for real power and flexibility if you want to automate your processes and generate hundreds or thousands of different QR codes in different directories.

06:10 When you’ve completed this course, if you’d like to learn more about pathlib, Real Python has a video course to take you through everything you need to understand path objects better.

06:21 In the next section of the course, you’ll take a look at something you were promised earlier, artistic QR codes with images in the background.

Become a Member to join the conversation.