/ friday fun

Friday Fun QR Code Madness!

alt
Yeah that's the Greggs logo in a QR code for this website

QR codes, (Quick Response), are all around us. We see them on adverts, newspapers, drinks bottles. They are a quick way for mobile users to access web content, as all you need is a camera and a barcode app to scan the code and visit the website.

How can you create your own QR code?

I found a simple Python library, that can be used from the command line to generate quick QR codes.

Install

On my Linux machine I used pip3 to install the python library.

sudo pip3 install myqr

A number of other libraries were also installed, but it only took a few minutes for the install to complete.

Using from the command line

alt

Open a terminal, on a Raspberry Pi the icon is in the top left of the screen. I wanted to create a link to my website, a simple QR code.

alt

So I typed

myqr http://bigl.es

This creates a QR code with my website address, and saves it in the current working directory as qrcode.png

But...lets add an image!

Adding an image is rather simple

myqr http://bigl.es/ -n bigles.jpg -p rebel.gif

We call the myqr application, use -n to specify the filename that we would like to save the QR code as, then use -p to specify the name of the image that we wish to use.

alt

Colours!!!!

Yes our QR code can have colours and it a simple matter of adding -c to the command to colourise.

myqr http://bigl.es/ -n bigles.jpg -p greggs.png -c

alt
Thanks for not suing Greggs, I love your sausage rolls

Animated!!!

Oh yes you can create an animated GIF QR code!
You need to ensure that the -n used to save the image is set output a GIF, then just use -p with an animated GIF...oh and use -c to ensure it is colourful!

myqr http://bigl.es/ -n bigles.gif -p sonic.gif -c

alt

Using from Python 3

As this is a Python 3 library, we can use this wonderful library in our Python projects. I used Python 3 editor, but you could also use Thonny, or Sublime Text, or Vi... (We'll assume that you know how to write Python code, and that you have a favourite editor.)

We use os is used to access the underlying filesystem

import os

Imports the myqr library.

from MyQR import myqr

We then specify the web address, or any text that we wish to encode in the QR code.

version, level, qr_name = myqr.run(
	"http://your-website-here",
    version=1,
    level='H',

Our picture is the image that we wish to use in the QR code.

    picture="/directory/where/the/image/is",

If we would like a coloured image, change False to True.

    colorized=False,

The save_name is the file into which we want to save the QR code image. Remember to use GIF if you want to create an animated GIF QR code.

    contrast=1.0,
    brightness=1.0,
    save_name="cool-qr-code.gif",

Lastly we set the save location to the current working directory, in other words the directory where you are running this code from.

    save_dir=os.getcwd()
	)

Complete Code Listing

import os
from MyQR import myqr
version, level, qr_name = myqr.run(
	"http://your-website-here",
    version=1,
    level='H',
    picture="/directory/where/the/image/is",
    colorized=False,
    contrast=1.0,
    brightness=1.0,
    save_name="cool-qr-code.gif",
    save_dir=os.getcwd()
	)

When ready save the code and run it to create your QR codes.

Taking it further

  • Creating a function to automate the options.
  • Create a GUI to enable anyone to easily create QR codes.

Have fun hacking your new QR codes!