Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update amzqr.py #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions amzqr/amzqr.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
# brightness: float
# save_name: str, the output filename like 'example.png'
# save_dir: str, the output directory
# resizemode: int, from 1 to 2
#
# See [https://github.com/hwxhw/amazing-qr] for more details!
def run(words, version=1, level='H', picture=None, colorized=False, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd()):
def run(words, version=1, level='H', picture=None, colorized=False, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd(), resizemode=1):

supported_chars = r"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ··,.:;+-*/\~!@#$%^&`'=<>[]()?_{}|"

Expand All @@ -31,9 +32,11 @@ def run(words, version=1, level='H', picture=None, colorized=False, contrast=1.0
raise ValueError('Wrong version! Please choose a int-type value from 1 to 40!')
if not isinstance(level, str) or len(level)>1 or level not in 'LMQH':
raise ValueError("Wrong level! Please choose a str-type level from {'L','M','Q','H'}!")
if not isinstance(resizemode, int) or resizemode not in range(1, 3):
raise ValueError('Wrong mode! Please choose a int-type value from 1 to 2! [1 = keep ratio, 2 = center crop]')
if picture:
if not isinstance(picture, str) or not os.path.isfile(picture) or picture[-4:] not in ('.jpg','.png','.bmp','.gif'):
raise ValueError("Wrong picture! Input a filename that exists and be tailed with one of {'.jpg', '.png', '.bmp', '.gif'}!")
if not isinstance(picture, str) or not os.path.isfile(picture) or picture[-4:] not in ('webp','jpeg','.jpg','.png','.bmp','.gif'):
raise ValueError("Wrong picture! Input a filename that exists and be tailed with one of {'webp', 'jpeg', '.jpg', '.png', '.bmp', '.gif'}!")
if picture[-4:] == '.gif' and save_name and save_name[-4:] != '.gif':
raise ValueError('Wrong save_name! If the picuter is .gif format, the output filename should be .gif format, too!')
if not isinstance(colorized, bool):
Expand All @@ -48,7 +51,7 @@ def run(words, version=1, level='H', picture=None, colorized=False, contrast=1.0
raise ValueError('Wrong save_dir! Input a existing-directory!')


def combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, save_name=None):
def combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, resizemode, save_name=None):
from amzqr.mylibs.constant import alig_location
from PIL import ImageEnhance, ImageFilter

Expand All @@ -59,10 +62,16 @@ def combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, sa
bg0 = ImageEnhance.Contrast(bg0).enhance(contrast)
bg0 = ImageEnhance.Brightness(bg0).enhance(brightness)

if bg0.size[0] < bg0.size[1]:
bg0 = bg0.resize((qr.size[0]-24, (qr.size[0]-24)*int(bg0.size[1]/bg0.size[0])))
if resizemode == 1:
#To resize with ratio
if bg0.size[0] < bg0.size[1]:
bg0 = bg0.resize((qr.size[0]-24, (qr.size[0]-24)*int(bg0.size[1]/bg0.size[0])))
else:
bg0 = bg0.resize(((qr.size[1]-24)*int(bg0.size[0]/bg0.size[1]), qr.size[1]-24))
else:
bg0 = bg0.resize(((qr.size[1]-24)*int(bg0.size[0]/bg0.size[1]), qr.size[1]-24))
#Crop to center
bg0 = crop_max_square(bg0)
bg0 = bg0.resize((qr.size[0]-24, qr.size[1]-24))

bg = bg0 if colorized else bg0.convert('1')

Expand All @@ -87,11 +96,22 @@ def combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, sa

tempdir = os.path.join(os.path.expanduser('~'), '.myqr')

#Center crop image
def crop_max_square(pil_img):
return crop_center(pil_img, min(pil_img.size), min(pil_img.size))

def crop_center(pil_img, crop_width, crop_height):
img_width, img_height = pil_img.size
return pil_img.crop(((img_width - crop_width) // 2,
(img_height - crop_height) // 2,
(img_width + crop_width) // 2,
(img_height + crop_height) // 2))

try:
if not os.path.exists(tempdir):
os.makedirs(tempdir)

ver, qr_name = theqrmodule.get_qrcode(version, level, words, tempdir)
ver, qr_name = theqrmodule.get_qrcode(version, level, words, tempdir, resizemode)

if picture and picture[-4:]=='.gif':
import imageio
Expand All @@ -116,7 +136,7 @@ def combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, sa
qr_name = os.path.join(save_dir, os.path.splitext(os.path.basename(picture))[0] + '_qrcode.gif') if not save_name else os.path.join(save_dir, save_name)
imageio.mimwrite(qr_name, ims, '.gif', **{ 'duration': duration/1000 })
elif picture:
qr_name = combine(ver, qr_name, picture, colorized, contrast, brightness, save_dir, save_name)
qr_name = combine(ver, qr_name, picture, colorized, contrast, brightness, save_dir, resizemode, save_name)
elif qr_name:
qr = Image.open(qr_name)
qr_name = os.path.join(save_dir, os.path.basename(qr_name)) if not save_name else os.path.join(save_dir, save_name)
Expand All @@ -129,4 +149,4 @@ def combine(ver, qr_name, bg_name, colorized, contrast, brightness, save_dir, sa
finally:
import shutil
if os.path.exists(tempdir):
shutil.rmtree(tempdir)
shutil.rmtree(tempdir)