Posts

Showing posts from November, 2022

Reading QR code in Python

We learned how to create QR code in last blog. Now, let's see how we can read these QR code using python.  Package Required:     pip install opencv-python We can use below code to read a QR code image.  import cv2 det = cv2 .QRCodeDetector() img_1 = cv2 .imread( "sample_1.png" ) val_1 , _ , _ = det .detectAndDecode( img_1 ) print ( "Sample_1 Data =" , val_1 ) if image is not in gray or RGB. it might not able to decode, For example I created a QR code with fill color as yellow and back color as black and it couldn't decode this code. One more step I tried to read this image in gray scale, picture was not very clear. May be due to this it couldn't decode.  After changing fill color as purple, green or red and back color as white, message was decoded successfully.  In order to read image in gray scale and save using cv2, we can use below code.  img_2 = cv2 .imread( "sample_2.png" , 0 ) cv2 .imwrite( 'gray_sample2.png' , img_2 ) Thank y

Create QR code using qrcode in python

We need to install qrcode module to create QR code using python. Use below command to install using pip. pip install qrcode  Now let's check below code to create a png image with some sample data.  import qrcode msg = "Welcome to Code Adhyayana!!!" img_qr = qrcode .make( msg ) img_qr .save( 'sample.png' ) Error: ModuleNotFoundError: No module named 'Image' if you are getting above module not found error, while executing this code then you need to install PIL module using below command: pip install Pillow for conda users: conda install -c anaconda Pillow after installing Pillow, re-execute the code, this time you should get sample.png image with QR code. We can scan and validate if it's correct or not.  Let's check how we can create qrcode with custom color and size.  import qrcode qr_obj = qrcode .QRCode( box_size = 20 ,                 border = 2 ) qr_obj .add_data( "Welcome to Code Adhyayana" ) qr_img = qr_obj .make_image( fill_