Skip to content

Instantly share code, notes, and snippets.

@hustlzp
Last active January 21, 2018 12:10

Revisions

  1. hustlzp revised this gist Dec 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion process_image.py
    Original 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 = avatar.resize((max_border, max_border), Image.ANTIALIAS)
    image = image.resize((max_border, max_border), Image.ANTIALIAS)

    # 保存
    ext = extension(file_storage.filename)
  2. hustlzp revised this gist Dec 14, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions process_image.py
    Original 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)
    avatar = image.crop(crop_region)
    image = image.crop(crop_region)

    # 缩放
    if border > max_border:
    avatar = avatar.resize((max_border, max_border), Image.ANTIALIAS)
    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)
    avatar.save(path)
    image.save(path)
    return filename
  3. hustlzp revised this gist Dec 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion process_image.py
    Original 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对象
    @max_border: 待缩放的边长
    @param max_border: 待缩放的边长
    @return: 图片filename
    """
    # 打开图片
  4. hustlzp revised this gist Dec 14, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion process_image.py
    Original 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)
    path = os.path.join(upload_set.config.destination, filename)
    folder = upload_set.config.destination
    filename = upload_set.resolve_conflict(folder, filename)
    path = os.path.join(folder, filename)
    avatar.save(path)
    return filename
  5. hustlzp revised this gist Dec 14, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion process_image.py
    Original 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)

  6. hustlzp created this gist Dec 14, 2013.
    44 changes: 44 additions & 0 deletions process_image.py
    Original 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