Artistic QR Codes

Segno focuses on creating (Micro) QR codes and offers many output formats without additional dependencies on other libraries.

Advanced graphic operations require the qrcode-artistic plug-in, which in turn depends on the Pillow library.

The plugin can be used to create animated QR codes or static QR codes with a background image.

To install the plugin, use:

pip install qrcode-artistic

After that, every QR code created with segno.make(), segno.make_qr() and segno.make_micro() provide two additional methods “to_pil” and “to_artistic”.

The former returns a Pillow Image instance, which can be used for further manipulations (e.g. rotating the QR code).

>>> import segno
>>> qr = segno.make('Yellow Submarine', error='h')
>>> img = qr.to_pil(scale=3).rotate(45, expand=True)
>>> img.save('yellow-submarine-rotated.png')
Colorful 3-H QR code encoding "Yellow Submarine" rotated by 45°

The “to_pil” method provides all options of Colorful QR Codes.

>>> import segno
>>> qr = segno.make('Yellow Submarine', error='h')
>>> img = qr.to_pil(scale=4, dark='darkred', data_dark='darkorange',
                    data_light='yellow')
>>> img.save('yellow-submarine.png')
Colorful 3-H QR code encoding "Yellow Submarine"

The “to_artistic” method can create animated or static QR codes.

>>> import segno
>>> qr = segno.make('The Beatles -- Albums', error='h')
>>> qr.to_artistic(background='src/albums.gif'), target='albums.gif' scale=8)
3-H QR code encoding "The Beatles -- Albums" (animated)

If the Pillow installation supports animated WebP images the plugin can save animated WebP images as well.

>>> import segno
>>> qr = segno.make('The Beatles -- Abbey Road', error='h')
>>> qr.to_artistic(background='src/abbey-road-walking.gif', target='abbey-road.webp' scale=4)
4-H QR code encoding "The Beatles -- Abbey Road" (animated)

The plugin also supports static backgrounds

>>> import segno
>>> qr = segno.make('The Beatles -- Let It Be', error='h')
>>> qr.to_artistic(background='src/letitbe.jpg', target='letitbe.jpg' scale=5)
3-H QR code encoding "The Beatles -- Let It Be" with a background image

It’s also possible to write the result into a io.BytesIO stream if the kind parameter is provided:

>>> import io
>>> import segno
>>> qr = segno.make('The Beatles -- Let It Be', error='h')
>>> out = io.BytesIO()
>>> qr.to_artistic(background='src/letitbe.jpg', target=out scale=5, kind='jpg')
3-H QR code encoding "The Beatles -- Let It Be" with a background image

If the background should be specified by a URL use a file-like object:

>>> from urllib.request import urlopen
>>> import segno
>>> qr = segno.make('Ringo Starr', error='h')
>>> url = 'https://media.giphy.com/media/HNo1tVKdFaoco/giphy.gif'
>>> bg_file = urlopen(url)
>>> qr.to_artistic(background=bg_file, target='ringo.gif', scale=10)
2-H QR code encoding "Ringo Starr" with a background image

It is possible to combine both techniques so that nothing is written to the hard drive:

>>> import io
>>> from urllib.request import urlopen
>>> import segno
>>> qr = segno.make('The Beatles', error='h')
>>> url = 'https://media.giphy.com/media/mUPQmck5YEisg/giphy.gif'
>>> bg_file = urlopen(url)
>>> out = io.BytesIO()
>>> qr.to_artistic(background=bg_file, target=out, scale=5, kind='gif')
2-H QR code encoding "The Beatles" with a background image