I have been exploring Python for some time and I wanted to create a Qr-code for some reason using Python. Here is how to create QR Images Using it.
- Python 2.7
- python-qrcode library (available from Github.com)
- Download the source code as zip file
- Right Click and Extract here the Zip file
- Open your terminal and navigate to the Extracted folder using ‘cd’ command
- Then run ‘sudo python setup.py install‘ (Enter your password when prompted)
- Open a new file
- Copy paste the below code.
data = raw_input(“Type something to create QRCode: “) #gets input from user to encode into qrcode
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=10,border=4,) #initialize settings for Output Qrcode
qr.add_data(data) #adds the data to the qr cursor
img = qr.make_image()
file_name = raw_input(“Name for the Output image file, just the name without any extension: “)
file_extension = raw_input(“What type of Image? (PNG/JPEG): “)
file_name = file_name+’.'+file_extension
image_file = open(file_name,’w+’) #will open the file, if file does not exist, it will be created and opened.
img.save(image_file,file_extension.upper()) #write qrcode encoded data to the image file.
image_file.close() #close the opened file handler.
3. Save the file with extension ‘.py’ and close the editor
4. Right click the file -> Properties -> Permissions -> Allow executing file as program
5. Double click the file, and click on ‘Run on Terminal’
Execution & Output
- Type something to create QrCode: Enter any data here that should be encoded into the qrcode image
- Name for the Output image file, just the name without any extension: testing
- What type of Image? (PNG/JPEG): PNG or JPEG (type any one)
That’s it. The Program ends and you should see a testing.PNG or testing.JPEG file inside the same folder.
You can view it with any Image viewer, You can Scan the image using QRCode scanner app available for Smartphones (After all that is the purpose for today).
The sample Qr-code should look like this