Free QR code generator using python | Cool python project

 

QR code generator using python

What is QRcode?

A QRcode is a square box optical label that is generated by matrix values with some black and white pixels that can hold up to 6,000 content text (numeric, binary, kanji, and alphanumeric). QRcode mostly contains data for an identifier that points to a link. QRcode is used to store data efficiently. For example,

(a) Get information about a product

(b) Connect with wifi without typing the password.

(c) Get the mobile number of someone else in a second by scanning.

stylish qrcode
A stylish QRcode

The full meaning of QRcode is "Quick Response code" and this name is given that the QRcode collects data (encrypted) quickly.

Why should a programmer learn the implementation of QRcode

QRcode codes have become very popular in the world because it is faster to capture a text link to a website address (an URL). QR codes are mostly used to scan phone numbers in different sectors of life. However, due to the corona epidemic, the QRcode has become more familiar to us due to its unique feature.

Nowadays restaurants are also using their menus in QRcode to make the management process faster and to reacher their customer easily. Take out your smartphone camera and scan the menu QRcode, the food menu immediately appear on your smartphone screen and order it easily. This is how QRcode has solved thousands of such problems in a second.

I think, you already read the above introduction part about the QRcode. Now, you understand the necessity of QRcode in this everyday life. You are a programmer and you need to know to learn the implementation of QRcode. This will help you to give your customer better quality and will make faster your mobile or desktop or web app.


QRcode generator using python

The implementation of QRcode is complicated and also oversized in length. So, it is better to use a pre-built library. For this, we recommend the 'python-qrcode', a pre-built library of python. As QRcode is an image type label and that is why you need to install the 'pillow' library. First of all, run the below command in the python shell or command shell in your device. It will automatically install the required 'pillow' package with a QRcode package.

Installation of 'qrcode' python package

In Windows run:

pip install qrcode[pil]


In macOS run:

pip install qrcode"[pil]"

A free QR code generator using python

The below will generate the QRcode for an input URL and output will be saved as a png image file. For saving the image, we use the pillow library.

import qrcode
import io

def GenerateQrcode(text):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
return qr.make_image(fill_color="black", back_color="white")

if __name__ == '__main__':
img = GenerateQrcode('schoolofbeginners.blogspot.com')
img.save('sob.png')


Output:



A simple qrcode using python
A simple QRcode using python


If you want to print the QRcode in the output shell as text content then add this in the above code. 

def PrintText(qr):
f = io.StringIO()
qr.print_ascii(out=f)
f.seek(0)
return print(f.read())


The input-output(io) package converts the symbols of generated QRcode as content text in the output of a python shell.


Output in the shell:

output
Output in python shell

Output as a text:


    █▀▀▀▀▀█ █▀▄▀  ▀▀▄ █▀▀▀▀▀█    

    █ ███ █ ██▄ ▀▄▄▄▀ █ ███ █    

    █ ▀▀▀ █ ▄█▄▀ ▀█▀▀ █ ▀▀▀ █    

    ▀▀▀▀▀▀▀ ▀ █ ▀ ▀ █ ▀▀▀▀▀▀▀    

    ▀▀  ▀▀▀▄▄ ▀▄▀ ▄▄█  ▀▄▀█▀▀    

    █▄██▀▄▀ ██▀▀▄ ▀█    ▀▀█ ▄    

    ▄▀▄▀▄▄▀█▀ ▄██ ▄▄▄▀▀▀▄▄█ ▀    

      ▀█  ▀█▀▀▀█▄▀█▀▀ ▄▄▀ ▄ ▄    

    ▀▀▀▀  ▀ █▀▀▄ ▄▄▀█▀▀▀█▄█▄▀    

    █▀▀▀▀▀█ ▄ █▀█▀█ █ ▀ █ ▄▄▄    

    █ ███ █ ▀▄██▀▄  ████▀▄██▄    

    █ ▀▀▀ █ ▄▀ █ ▀▄▄ ███▄▀██     

    ▀▀▀▀▀▀▀ ▀▀     ▀ ▀▀   ▀▀▀ 



Finally, the code will be like this

import qrcode
import io

# get the text content of the generated qrcode
def PrintText(qr):
f = io.StringIO()
qr.print_ascii(out=f)
f.seek(0)
return print(f.read())

def GenerateQrcode(text):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
PrintText(qr)
return qr.make_image(fill_color="black", back_color="white")

if __name__ == '__main__':
img = GenerateQrcode('schoolofbeginners.blogspot.com')
img.save('sob.png')


You can change the color in the QRcode image by changing the below line. (Now, you want a red or green QRcode)

qr.make_image(fill_color="red", back_color="white")
qr.make_image(fill_color="green", back_color="white")


Output:

a red QRcode
A red QRcode

A green QRcode
A green QRcode


QR code with logo in the middle using python

import qrcode
from qrcode.image.styledpil import StyledPilImage

qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data('schoolofbeginners.blogspot.com')

img_3 = qr.make_image(image_factory=StyledPilImage, embeded_image_path="sob.png")
img_3.save('logo_with_QRcode.png')

Output:
logo in the middle QRcode
Logo with QRcode


Generate stylish QR code using python

import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers import RoundedModuleDrawer
from qrcode.image.styles.colormasks import RadialGradiantColorMask

qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data('schoolofbeginners.blogspot.com')

img_1 = qr.make_image(image_factory=StyledPilImage, module_drawer=RoundedModuleDrawer())
img_1.save('round.png')

img_2 = qr.make_image(image_factory=StyledPilImage, color_mask=RadialGradiantColorMask())
img_2.save('radial.png')

Outputs:

round qrcode
Round style QRcode

radial color QRcode
Radial colorful QRcode

NoteBook: Read the full documentation page from Github.


Post a Comment

Previous Post Next Post