1
0
mirror of https://github.com/x-hw/amazing-qr.git synced 2025-07-15 01:24:37 +02:00

update code about exceptions

This commit is contained in:
sylnsfar
2016-09-05 22:36:03 +08:00
parent 0138ebee03
commit cb87de0e88

22
myqr.py
View File

@ -2,6 +2,7 @@
import argparse, os import argparse, os
from mylibs import theqrmodule from mylibs import theqrmodule
from PIL import Image
# Alignment Pattern Locations # Alignment Pattern Locations
alig_location = [ alig_location = [
@ -19,7 +20,7 @@ argparser.add_argument('-bri', '--brightness', type = float, help = 'A floating
args = argparser.parse_args() args = argparser.parse_args()
def combine(qr_name, bg_name, colorized, contrast, brightness, save_place): def combine(qr_name, bg_name, colorized, contrast, brightness, save_place):
from PIL import Image, ImageEnhance, ImageFilter from PIL import ImageEnhance, ImageFilter
qr = Image.open(qr_name) qr = Image.open(qr_name)
qr = qr.convert('RGBA') if colorized else qr qr = qr.convert('RGBA') if colorized else qr
@ -53,9 +54,10 @@ def combine(qr_name, bg_name, colorized, contrast, brightness, save_place):
qr.putpixel((i+12,j+12), bg.getpixel((i,j))) qr.putpixel((i+12,j+12), bg.getpixel((i,j)))
qr_name = os.path.join(save_place, os.path.splitext(bg_name)[0] + '_qrcode.png') qr_name = os.path.join(save_place, os.path.splitext(bg_name)[0] + '_qrcode.png')
qr.resize((qr.size[0]*2, qr.size[1]*2)).save(qr_name) qr.resize((qr.size[0]*3, qr.size[1]*3)).save(qr_name)
return qr_name return qr_name
try:
# the default version depends on WORDs and level # the default version depends on WORDs and level
# init as 0 # init as 0
ver = args.version if args.version else 0 ver = args.version if args.version else 0
@ -65,12 +67,14 @@ ecl = args.level if args.level else 'H'
if not os.path.exists('temp'): if not os.path.exists('temp'):
os.makedirs('temp') os.makedirs('temp')
save_place = os.path.abspath('.') if not args.picture else os.path.join(os.path.abspath('.'), 'temp') save_place = os.path.abspath('.') if not args.picture else os.path.join(os.path.abspath('.'), 'temp')
ver, qr_name = theqrmodule.get_qrcode(ver, ecl, args.WORDs, save_place, bool(args.picture)) try:
ver, qr_name = theqrmodule.get_qrcode(ver, ecl, args.WORDs, save_place)
except TypeError:
qr_name = args.picture = None
if args.picture and args.picture[-4:]=='.gif': if args.picture and args.picture[-4:]=='.gif':
print('it takes a while, please wait for minutes...') print('it takes a while, please wait for minutes...')
from PIL import Image
import imageio import imageio
im = Image.open(args.picture) im = Image.open(args.picture)
@ -93,9 +97,15 @@ if args.picture and args.picture[-4:]=='.gif':
imageio.mimsave(qr_name, ims) imageio.mimsave(qr_name, ims)
elif args.picture: elif args.picture:
qr_name = combine(qr_name, args.picture, args.colorized, args.contrast, args.brightness, os.path.abspath('.')) qr_name = combine(qr_name, args.picture, args.colorized, args.contrast, args.brightness, os.path.abspath('.'))
elif qr_name:
qr = Image.open(qr_name)
qr.resize((qr.size[0]*3, qr.size[1]*3)).save(qr_name)
if qr_name:
print('Succeed! \nCheck out your ' +str(ver) + '-' + str(ecl) + ' QR-code at', qr_name)
except:
raise
finally:
import shutil import shutil
if os.path.exists('temp'): if os.path.exists('temp'):
shutil.rmtree('temp') shutil.rmtree('temp')
if qr_name is not None:
print('Succeed! \nCheck out your ' +str(ver) + '-' + str(ecl) + ' QR-code at', qr_name)