Last active
January 21, 2018 12:10
Revisions
-
hustlzp revised this gist
Dec 14, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,7 @@ def process_image(file_storage, upload_set, max_border): # 缩放 if border > max_border: image = image.resize((max_border, max_border), Image.ANTIALIAS) # 保存 ext = extension(file_storage.filename) -
hustlzp revised this gist
Dec 14, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,17 +29,17 @@ def process_image(file_storage, upload_set, max_border): else: border = w crop_region = (0, (h - border) / 2, border, (h + border) / 2) image = image.crop(crop_region) # 缩放 if border > max_border: image = avatar.resize((max_border, max_border), Image.ANTIALIAS) # 保存 ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) folder = upload_set.config.destination filename = upload_set.resolve_conflict(folder, filename) path = os.path.join(folder, filename) image.save(path) return filename -
hustlzp revised this gist
Dec 14, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ def process_image(file_storage, upload_set, max_border): @param file_storage: Werkzeug FileStorage对象, 如request.files['image'] @param upload_set: Flask-Uploads UploadSet对象 @param max_border: 待缩放的边长 @return: 图片filename """ # 打开图片 -
hustlzp revised this gist
Dec 14, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,6 +38,8 @@ def process_image(file_storage, upload_set, max_border): # 保存 ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) folder = upload_set.config.destination filename = upload_set.resolve_conflict(folder, filename) path = os.path.join(folder, filename) avatar.save(path) return filename -
hustlzp revised this gist
Dec 14, 2013 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,6 @@ def process_image(file_storage, upload_set, max_border): @max_border: 待缩放的边长 @return: 图片filename """ # 打开图片 image = Image.open(file_storage.stream) -
hustlzp created this gist
Dec 14, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ import os import uuid from PIL import Image from flask.ext.uploads import extension def random_filename(): """生成伪随机uuid字符串,用做文件名""" return str(uuid.uuid4()) def process_image(file_storage, upload_set, max_border): """ 将Flask中上传的图片进行居中裁剪、缩放,然后保存 @param file_storage: Werkzeug FileStorage对象, 如request.files['image'] @param upload_set: Flask-Uploads UploadSet对象 @max_border: 待缩放的边长 @return: 图片filename """ # 打开图片 image = Image.open(file_storage.stream) # 居中裁剪 w, h = image.size if w > h: border = h crop_region = ((w - border) / 2, 0, (w + border) / 2, border) else: border = w crop_region = (0, (h - border) / 2, border, (h + border) / 2) avatar = image.crop(crop_region) # 缩放 if border > max_border: avatar = avatar.resize((max_border, max_border), Image.ANTIALIAS) # 保存 ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) path = os.path.join(upload_set.config.destination, filename) avatar.save(path) return filename