Rotating a QR Code
00:00
Rotating the QR code. With segno
, you are also able to manipulate your QR code by rotating it or adding an image to the background. If you’d like to create QR codes with advanced graphical operations, then you will need to install the dependencies, qrcode artistic
, and pillow
.
00:28
With these dependencies installed, you will be able to use two additional methods, .to_pil()
and .to_artistic()
when creating your QR code with make_qr()
.
00:39 This will convert your QR code into a pillow image instance, which you can use to rotate the QR code or replace the static background with an image as you will see later in the course.
00:50
But for now, if you would like to rotate your QR code, then you will use the .to_pil()
method and specify the rotation angle in degrees. On screen, you can see how to do this with your trusty “Hello, World” text.
01:07
You have to specify the degree of rotation within the .to_pil().rotate()
method. The direction of rotation is counterclockwise. By using the .to_pil()
method and specifying the degree of rotation Ii the .rotate()
method, you have written the code to create a black and white QR code object rotated by 45 degrees.
01:26
Finally, you apply the .save()
method to the QR code rotated variable.
01:34
Now it is time to run rotated_qr_code
from the command line and see what you get.
01:49 It is a little small since you haven’t adjusted the size, but you can now scan the rotated QR code image as seen on screen. But you may have noticed the observed QR code is truncated.
02:02
The corners of the quiet zone have been cut off. This is probably more noticeable if you have colors in play. If you would like to keep the whole image during rotation, then you can add an expand
argument in the .to_pil().rotate()
method and set it to True
.
02:20 Run the script once more, and you should see the expanded QR code without truncated corners.
02:28
You may want to adjust the size of the rotated QR code or change the background color as you did earlier in the course, but if you want to format a rotated QR code, adding the scale
or light
argument to the .save()
method won’t change the size or background color of the code.
02:43
While the object looks like it did before, you are actually calling .save()
on a PIL object and not a QR code object. Because of this, you have to add the parameters that resize and change the colors of the QR code within the .to_pil()
method instead. Here you add a scale factor of five to the QR code object, change the color of the light parts of the QR code to light blue and the dark parts to green.
03:19 Run this code to generate the QR code,
03:24 and you should be able to scan the image generated on screen.
03:32
You may have noticed that the rotated QR code has some additional blank space that’s not the same as the quiet zone around the QR code. This additional background is created to overlay the rotated QR code. To modify the colors of the dark modules and the additional border, which is black by default, you can adjust the value of the dark
argument in the .to_pil()
method to a color of your choice.
03:58
Not only can you change the background color of the QR code, but you can also replace it with an image. Later in the course, you will use the .to_artistic()
method to replace the plain background with an image and also create a QR code with an animated background.
04:14
So far, you’ve generated codes in PNG format only, but segno
supports multiple other file formats, and that is what you will be looking at in the next section of the course.
Become a Member to join the conversation.