My personal blog around GNU/Linux


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.

Requirements

  1. Python 2.7
  2. 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)

Code

  1. Open a new file
  2. Copy paste the below code.

#!/usr/bin/python2.7
import qrcode

data = raw_input(“Type something to create QRCode: “) #gets input from user to encode into qrcode
print “\n”

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

qr.make(fit=True)
img = qr.make_image()

file_name = raw_input(“Name for the Output image file, just the name without any extension: “)
print “\n”
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’

run

 

Execution & Output

  1. Type something to create QrCode: Enter any data here that should be encoded into the qrcode image
  2. Name for the Output image file, just the name without any extension: testing
  3. 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

test

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: