前言
我目前是Python小菜鸟,在此存储一下使用Python来操作图片。
完整代码如下
#导入Image这个库
from PIL import Image
#打开相关图片的路径
im = Image.open('/Users/lulei/Desktop/Image/test.jpg')
#获取图片的大小
w,h = im.size
#打印输出图片的大小
print('Original image size: %sx%s' % (w, h))
#压缩图片
im.thumbnail((h//2,w//2))
print('Resize image to: %sx%s'%(w//2,h//2))
#旋转图片
te=im.rotate(-90,expand=True)
#保存压缩与旋转之后的图片
te.save('thumbnail.jpg','jpeg')
问题一、
没有安装Pillow模块,导致找不到Image这个库
解决方法为安装:
pip install pillow
问题二、
旋转时碰到裁切问题,如下图

原图为:

解决方法
在旋转图片时,设置expand=True,关闭裁剪。
te=im.rotate(-90,expand=True)
旋转并关闭裁剪效果之后

使用Python判断某个文件是否存在

import os.path
网友评论