如果你的系统没有安装Pillow模块,安装:

 
pip install pillow

 

逆时针方向旋转

# 导入 Pillow:
from PIL import Image
 
# 加载原始图片
img = Image.open("rubberwhale1.png")
 
# 逆时针方向旋转
img2 = img.rotate(45)
img2.save("rubberwhale2.jpg")
img3 = img.rotate(90)
img3.save("rubberwhale3.jpg")

Python中用PIL/Pillow旋转图片

Python中用PIL/Pillow旋转图片

Python中用PIL/Pillow旋转图片

 

顺时针旋转

img4 = img.rotate(-45)
img4.save("rubberwhale4.jpg")

 

Python中用PIL/Pillow旋转图片

 

关闭裁剪

img5 = img.rotate(45, expand=True)
img5.save("rubberwhale5.jpg")

Python中用PIL/Pillow旋转图片

 

重采样过滤

近邻取样(默认)

 
img6 = img.rotate(45, resample=Image.NEAREST)
img6.save("rubberwhale6.jpg")
 

Python中用PIL/Pillow旋转图片

 

线性插值

img7 = img.rotate(45, resample=Image.BILINEAR)
img7.save("rubberwhale7.jpg")

Python中用PIL/Pillow旋转图片

 

三次样条插值

img8 = img.rotate(45, resample=Image.BICUBIC)
img8.save("rubberwhale8.jpg")

Python中用PIL/Pillow旋转图片

相关文章: