Created
January 11, 2014 05:56
-
-
Save jamiesun/8367587 to your computer and use it in GitHub Desktop.
PIL压缩图片
This file contains 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
def resize_img(img_path, out_path, new_width): | |
import Image | |
#读取图像 | |
im = Image.open(img_path) | |
#获得图像的宽度和高度 | |
width,height = im.size | |
#计算高宽比 | |
ratio = 1.0 * height / width | |
#计算新的高度 | |
new_height = int(new_width * ratio) | |
new_size = (new_width, new_height) | |
#插值缩放图像, | |
out = im.resize(new_size, Image.ANTIALIAS) | |
#保存图像 | |
out.save(out_path) | |
resize_img('t2.jpg', 't2_ss.jpg', 750) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment