Linux Format

Custom QR creator..............

Les Pounder presents a straightfo­rward method for generating your own QR codes together with embedded images, using a custom interface.

-

Les Pounder shows us how to create your own QR codes with embedded images.

Quick Response codes − or QR for short − have been with us for some time now. They can be used to encode web addresses, phone numbers and even virtual business cards (Vcards). In this month’s project we shall code an applicatio­n to create our own QR code that contain a website address. Perhaps more interestin­gly, embedded inside the QR code we shall have a colour image to create our own QR code art! Furthermor­e, so that anyone can make their own QR codes we shall create a simple user interface using easygui. So let’s get started!

Pip pip pip

Installing the Python 3 libraries for this project is our first task. Open a Terminal and enter the following command: $ sudo pip3 install myqr easygui

Installati­on will take only a few moments and when it’s finished you can close the terminal.

From the main menu, go to the Programmin­g menu and click Python3 to launch the IDLE3 Python editor. You’ll see a Python interactiv­e shell (REPL, Read, Eval, Print, Loop) open. In this shell click File>New to create a new blank document, and then click File>Save and call the project QRCodeGene­ratory.py. Remember to save your work often.

We start the code for the project by importing the two libraries that we’ve just installed. First we import the easygui library as “eg” − this is to make it easier to use in our code. Second, we import the MyQR library that will generate the QR codes for our project: import easygui as eg from MyQR import myqr

In order to create our QR code we need to follow a series of steps, and the best place to store these steps is inside a function. Called “QRCreator”, this function has three parameters that can be passed when calling the function. These parameters are the URL we wish to encode, the image that we wish to embed in the QR code and finally the filename to save the QR code as: def QRCreator(URL, image, save):

Now inside of our function, we can identify this as the code that will be indented, so we need to create the steps to be taken when creating the QR code. First we use the “version” and “level” parameters to create a level of error correction, because QR codes are heavily dependant on the encoding being correct, otherwise a malformed or incorrect QR code is generated. This data is saved as an object that we can call. When the object is called we pass it the URL that we wish to encode, and then set the error correction settings (version and level): version, level, qr_name = myqr.run( URL, version=1, level='H’, In order to embed an image we have to tell MyQR where to find the picture, and in this case it’ll be stored in the “image”

parameter. If we want the image to be colour then we set “colorized” to True, else for black and white we set it to False. Then we set the brightness and contrast for the QR code.

picture=image, colorized=True, contrast=1.0, brightness=1.0,

In order to save the QR code as a file we need to create a filename. This parameter is passed when the function is called and used by the “save_name” variable. Then we set the save directory to be the /home/pi/ directory, and this can be changed to match your own preference.

save_name=save, save_dir="/home/pi/” )

The last line of our function uses the easygui library and calls the “msgbox” function, which generates a message box that communicat­es informatio­n to the user. In this case it tells us that the QR code has been created and that it can be found in the location that’s been specified.

eg.msgbox(msg="Your QR code has been saved to /home/ pi/"+save, title="Save Complete")

With the function complete our code is now free of an indentatio­n and it aligns to the left of the window. Our next line is to create a variable called “URL” that will store the output of calling the easygui enterbox function, which generates a dialog that we can use to obtain text input from the user. In this case it will be the web address (URL) that they wish to encode.

Note that the URL should start with http:// otherwise it may not work properly. The enterbox function has parameters that control the message (msg) to the user, and the title of the dialog. We can also pass additional parameters to embed an image in the dialog, but easygui only supports GIF images. Once the URL has been captured, we print the value stored in our URL variable to the Python shell for debug purposes.

URL = eg.enterbox(msg="Please provide the URL for the QR code”, title="Specify URL for QR code") print(URL)

To embed an image into the QR code we need to tell the Python code where to find it. Now of course we can pass a hard coded string that tells Python exactly where to find the file, but for new users this may be a bit too daunting. So by using easygui we can generate a simple File Open dialog that enables us to navigate the filesystem and select the correct image file. Note the file extension of the image (gif, jpg, png) as we’ll need to encode our QR code in the same extension. In order to open a default directory we set the default parameter to that of the user’s home directory. Finally, we print the value that’s stored in the image variable for debug purposes.

image = eg.fileopenbo­x(msg="What image shall I use with the QR code?”, title="Open Image”, default="/home/pi/") print(image)

The next two lines create a File Save box that we use to capture the filename that we wish to save the QR code as. This will also choose the user’s home directory as the default directory to store the image. Again, we print the value that’s stored in the save variable for debug.

save = eg.filesavebo­x(msg="Save QR Code as ?”, default="/ home/pi/") print(save)

The last line of code in this project calls the QRCreator function and passes it to the three parameters (URL, image, save) that we’ve created and stored in the correspond­ing variables. This then triggers the function to generate our QR code with an embedded image.

QRCreator(URL, image, save)

To run the code click Run>Run Module, then follow the steps to create your own QR codes with images. Let us know how you get on, or just email us your QR code!

 ??  ??
 ??  ?? You can pass any web address to this dialog, but ensure that you always start with http://, otherwise there may be an error when the QR code is generated.
You can pass any web address to this dialog, but ensure that you always start with http://, otherwise there may be an error when the QR code is generated.
 ??  ?? Generating our own QR codes with embedded images is now as easy as clicking a few buttons, thanks to our custom GUI.
Generating our own QR codes with embedded images is now as easy as clicking a few buttons, thanks to our custom GUI.

Newspapers in English

Newspapers from Australia