Skip to content

Instantly share code, notes, and snippets.

@jamiesun
Created January 11, 2014 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiesun/8367587 to your computer and use it in GitHub Desktop.
Save jamiesun/8367587 to your computer and use it in GitHub Desktop.
PIL压缩图片
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