Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Created January 15, 2019 08:32
Show Gist options
  • Save ayuLiao/7a8d3ffc456bff88ca985f388e5d62a1 to your computer and use it in GitHub Desktop.
Save ayuLiao/7a8d3ffc456bff88ca985f388e5d62a1 to your computer and use it in GitHub Desktop.
python生成缩略图
from PIL import Image as ImagePIL
def gen_thumb_image(path, width=0, height=0, filetype='JPEG'):
'''
生成缩略图
'''
width = min(1024, width)
height = min(1024, height)
img = ImagePIL.open(path)
if width and not height:
height = float(width) / img.size[0] * img.size[1]
if not width and height:
width = float(height) / img.size[1] * img.size[0]
stream = BytesIO()
img.thumbnail((width, height), ImagePIL.ANTIALIAS)
img.save(stream, format=filetype, optimize=True)
return stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment