qr code generator in python
This page is mainly about qr code generator in python
Perquisites
Pillow image manipulation library with
> pip install pillow
Python library, qrcode with
> pip install qrcode
1
Generate a sequence of codes using Python list comprehension.
There are various ways that you can do this but, by far the simplest is with a list comprehension. Here are some of my favourites.
Numerical codes
>>> [i for i in range(100)]
Numerical codes, left padded
>>> [str(i).zfill(8) for i in range(100)]
Hexadecimal codes
>>> [str(hex(i))[2:].zfill(8) for i in range(100)]
Hexadecimal codes, left padded
>>> [str(hex(i))[2:].upper().zfill(len(str(hex(upper))[2:]))+suffix for i in range(lower,upper+1)]
The Script
Probably a bit more complicated than it needs to be, but that's the story of my life.
import random
import os
import qrcode
import time
import csv
def validate(msg,options=[]):
valid = False
while not valid:
value = input(msg)
if (len(options) > 0):
if value in options:
return value
else:
print('Not valid choice. Try again.')
else:
return value
if __name__ == "__main__":
# Get parameters
choice_list = validate('Input - (l)ist or (g)enerate : ',['l','g'])
if choice_list == 'g':
codetype = validate('Type of code - (n)umerical or (h)ex : ',['n','h'])
padding = validate('Left pad (y/n) : ',['y','n'])
lower = int(input('Lower (int) : '))
valid = False
while not valid:
upper = int(input('Upper (int) : '))
if upper >= lower:
valid = True
else:
print('Upper can\t be less than lower. Try again.')
prefix = input('Prefix : ')
suffix = input('Suffix : ')
# Define generators
generators = {
'ny':'[prefix+str(i)+suffix for i in range(lower,upper+1)]',
'ny':'[prefix+str(i).zfill(len(str(upper)))+suffix for i in range(lower,upper+1)]',
'hn':'[prefix+str(hex(i))[2:].upper()+suffix for i in range(lower,upper+1)]',
'hy':'[prefix+str(hex(i))[2:].upper().zfill(len(str(hex(upper))[2:]))+suffix for i in range(lower,upper+1)]',
}
codes = eval(generators[codetype+padding])
else: # choice_list must be l
valid = False
while not valid:
filename = input('Filename : ')
if os.path.isfile(filename):
handle = open(filename,'r')
codes = handle.readlines()
handle.close()
for i in range(len(codes)):
codes[i] = codes[i].rstrip('\n')
valid = True
else:
print('File not found. Try again...')
directory = 'codes'
size = int(input('Side length (pixels) : '))
# Clear directory
print('Warning - all files in \''+directory+'\' will be deleted...')
input('Press ENTER if you are sure...')
files_to_delete = os.listdir(directory)
for file in files_to_delete:
full_path = os.path.abspath(directory+'\\'+file)
if os.path.isfile(full_path):
os.unlink(full_path)
files = [['code','picture']]
for code in codes:
print('Generating QR code for \'{0}\'...'.format(code))
qr = qrcode.QRCode(
version = 1,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size = 5,
border = 0
)
qr.add_data(code)
qr.make()
img = qr.make_image()
img = img.resize((size,size))
path = os.path.abspath(directory)+'\\'+str(time.time()).replace('.','')+'.png'
img.save(path)
files.append([code,path])
csv.sequence2csv(files,'list.csv')
Last modified: October 5th, 2021