You've already forked amazing-qr
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:
94
myqr.py
94
myqr.py
@ -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,49 +54,58 @@ 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
|
||||||
|
|
||||||
# the default version depends on WORDs and level
|
try:
|
||||||
# init as 0
|
# the default version depends on WORDs and level
|
||||||
ver = args.version if args.version else 0
|
# init as 0
|
||||||
# the default level is Q
|
ver = args.version if args.version else 0
|
||||||
ecl = args.level if args.level else 'H'
|
# the default level is Q
|
||||||
|
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)
|
im.save('temp/0.png')
|
||||||
im.save('temp/0.png')
|
while True:
|
||||||
while True:
|
try:
|
||||||
try:
|
seq = im.tell()
|
||||||
seq = im.tell()
|
im.seek(seq + 1)
|
||||||
im.seek(seq + 1)
|
im.save('temp/%s.png' %(seq+1))
|
||||||
im.save('temp/%s.png' %(seq+1))
|
except EOFError:
|
||||||
except EOFError:
|
break
|
||||||
break
|
|
||||||
|
imsname = []
|
||||||
imsname = []
|
for s in range(seq+1):
|
||||||
for s in range(seq+1):
|
bg_name = 'temp/%s.png' % s
|
||||||
bg_name = 'temp/%s.png' % s
|
imsname.append(combine(qr_name, bg_name, args.colorized, args.contrast, args.brightness, ''))
|
||||||
imsname.append(combine(qr_name, bg_name, args.colorized, args.contrast, args.brightness, ''))
|
|
||||||
|
ims = [imageio.imread(pic) for pic in imsname]
|
||||||
ims = [imageio.imread(pic) for pic in imsname]
|
qr_name = os.path.join(os.path.abspath('.'), os.path.splitext(args.picture)[0] + '_qrcode.gif')
|
||||||
qr_name = os.path.join(os.path.abspath('.'), os.path.splitext(args.picture)[0] + '_qrcode.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)
|
||||||
import shutil
|
qr.resize((qr.size[0]*3, qr.size[1]*3)).save(qr_name)
|
||||||
if os.path.exists('temp'):
|
|
||||||
shutil.rmtree('temp')
|
if qr_name:
|
||||||
if qr_name is not None:
|
print('Succeed! \nCheck out your ' +str(ver) + '-' + str(ecl) + ' QR-code at', qr_name)
|
||||||
print('Succeed! \nCheck out your ' +str(ver) + '-' + str(ecl) + ' QR-code at', qr_name)
|
except:
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
import shutil
|
||||||
|
if os.path.exists('temp'):
|
||||||
|
shutil.rmtree('temp')
|
Reference in New Issue
Block a user