use_pil_resize.py 403 B

123456789101112131415
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from PIL import Image
  4. # 打开一个jpg图像文件,注意是当前路径:
  5. im = Image.open('test.jpg')
  6. # 获得图像尺寸:
  7. w, h = im.size
  8. print('Original image size: %sx%s' % (w, h))
  9. # 缩放到50%:
  10. im.thumbnail((w//2, h//2))
  11. print('Resize image to: %sx%s' % (w//2, h//2))
  12. # 把缩放后的图像用jpeg格式保存:
  13. im.save('thumbnail.jpg', 'jpeg')