Created
January 15, 2019 08:32
-
-
Save ayuLiao/7a8d3ffc456bff88ca985f388e5d62a1 to your computer and use it in GitHub Desktop.
python生成缩略图
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 characters
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