QR Code Generator with Python
- November 7, 2022
- Posted by: Code and Debug
- Category: Mini Python Project Project

In this blog, we will learn how to generate QR Code using Python with help of qrcode module.
What is a QR Code?
Often when transacting with digital wallets, we scan the QR Code. Not just any code, QR Code is able to speed up and simplify the transaction process. The existence of a QR Code supports cardless transactions which are now echoed everywhere. Exactly what is a QR Code and how does it work?
QR Code is a matrix code or two-dimensional barcode derived from the word “Quick Response”, where the contents of the code can be deciphered quickly and precisely.
Steps to install qrcode module
- Install qrcode library
pip install qrcode
- Import qrcode
import qrcode
Basic Usage
Refer the code below to generate a basic QR Code.
import qrcode
img = qrcode.make("https://codeanddebug.in")
img.save("myQRcode.png")
A file with name myQRcode.png will be generated within the same folder.

Some more usage of qrcode module
Above, we created a QR code in its default form with default parameters, now we will try to give color, box size, border and more to our QR code.
import qrcode
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=15, border=2)
qr.add_data('https://codeanddebug.in')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="purple")
img.save("myQRcode2.png")
- ERROR_CORRECT_L: up to 7% or less errors can be corrected.
- ERROR_CORRECT_M: up to 15% or less errors can be corrected.
- ERROR_CORRECT_Q: up to 25% or less errors can be corrected.
- ERROR_CORRECT_H: up to 30% or less errors can be corrected.
A file with name myQRcode2.png will be generated within the same folder.

Adding round style to QR Code
We can style our QR Code using the StyledPilImage submodule.
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=15, border=2)
qr.add_data('https://codeanddebug.in')
qr.make(fit=True)
img = qr.make_image(image_factory= StyledPilImage, module_drawer=RoundedModuleDrawer())
img.save("myQRcode3.png")
A file with name myQRcode3.png will be generated within the same folder.

Adding gradient style to QR Code
We can style our QR Code using the RadialGradiantColorMask submodule.
import qrcode
from qrcode.image.styles.colormasks import RadialGradiantColorMask
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=15, border=2)
qr.add_data('https://codeanddebug.in')
qr.make(fit=True)
img = qr.make_image(image_factory= StyledPilImage, color_mask=RadialGradiantColorMask())
img.save("myQRcode4.png")
A file with name myQRcode4.png will be generated within the same folder.
