Generate QR Code image in Python using qrcode
This Python qrcode tutorial to show you how to create a QR code image in Python using the qrcode package. Via different Python example programs we learn how to use QR code image generator library to generate QR code with simple step or custom box size, border and colors.
Table of contents
- Install qrcode package
- Simple step to generate QR code in Python
- Generate QR code with specified box size
- Generate QR code with specified border
- Generate QR code with specified color and background color
- Generate QR code with custom box size, border and colors
Install qrcode package
Installing the QR code generator package using the following command which will include pillow package for generating image.
pip install qrcode[pil]
Simple step to generate QR code in Python
In the following Python program we show you how to generate simple QR code using qrcode package to generate QR code of data ‘https://toricode.com/'
import qrcode
data = 'https://toricode.com/'
qr_image = qrcode.make(data)
qr_image.save('qrcode1.png')
Execute the program above it will generate QR code as an image named qrcode1.png as below.
Generate QR code with specified box size
The parameter box_size to specified how many pixels each box of the QR code is. By default the qrcode package define 10 as default box size.
In the following Python program we show you how to generate QR code with a given box_size with encode the same data ‘https://toricode.com/'
import qrcode
data = 'https://toricode.com/'
# generate QR code with box size 100
qr = qrcode.QRCode(box_size=100)
qr.add_data(data)
qr_image = qr.make_image()
qr_image.save('qrcode2.png')
Execute the program above it will generate QR code as an image named qrcode2.png as below which bigger than the previous QR code image.
Generate QR code with specified border
By default the qrcode package set border for output QR code image is 4. In the following Python program we generate a QR code with given border which is smaller border.
import qrcode
data = 'https://toricode.com/'
# generate QR code with border 1
qr = qrcode.QRCode(border=1)
qr.add_data(data)
qr_image = qr.make_image()
qr_image.save('qrcode3.png')
Execute the program above it will generate QR code as an image named qrcode3.png as below which bigger than the previous QR code image.
Generate QR code with specified color and background color
The qrcode package also provide API to generate QR code with given color and background color via parameter fill_color and back_color as following Python program.
import qrcode
data = 'https://toricode.com/'
qr = qrcode.QRCode()
qr.add_data(data)
# make QR code image with colors
qr_image = qr.make_image(fill_color = "blue", back_color='grey')
qr_image.save('qrcode4.png')
Execute the program above it will generate QR code as an image named qrcode4.png as below which bigger than the previous QR code image.
We also can provide color via RGB code as Python program below.
import qrcode
data = 'https://toricode.com/'
qr = qrcode.QRCode()
qr.add_data(data)
# make QR code image with colors
qr_image = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35))
qr_image.save('qrcode5.png')
Execute the program above it will generate QR code as an image named qrcode5.png as below which bigger than the previous QR code image.
We also can provide colors via hexadecimal form as Python program below.
import qrcode
qr = qrcode.QRCode()
qr.add_data('https://toricode.com/')
# make QR code image with colors
qr_image = qr.make_image(back_color="#AFEEEE", fill_color="#FF1493")
qr_image.save('qrcode6.png')
Execute the program above it will generate QR code as an image named qrcode6.png as below which bigger than the previous QR code image.
Generate QR code with custom box size, border and colors
The following Python example we use three parameters above to generate QR code.
import qrcode
data = 'https://toricode.com/'
qr = qrcode.QRCode(box_size=60, border=1)
qr.add_data(data)
qr_image = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35))
qr_image.save('qrcode7.png')
Execute the program above it will generate QR code as an image named qrcode7.png as below which bigger than the previous QR code image.
Happy Coding 😊