网上经常能看到各种图片的翻转效果,今天我们用 python 来实现对一张图片进行各种角度的翻转。
还是用到了 python 中那个强大的图片处理库 pillow ,实现过程很简单,这里就不再过多说明了,直接看代码吧,代码中有注释。
# -*- coding: utf-8 -*-
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
#引入本地字体文件,用于下面title的打印,不然中文会有乱码
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
#打开图片(这里替换成你自己电脑本地的图片)
img = Image.open('tim2.jpg')
#subplot是指将多张图片在同一张画布上显示,前两个参数分别是多少行多少列,第三个参数是指第几张图片
#如 subplot(3,3,2) 表示 3*3 共排9张图片,当前是第二张图片
#title 是指图片的标题
#逆时针旋转90
dst1 = img.transpose(Image.ROTATE_90)
plt.subplot(3,3,1)
plt.title('逆时针旋转90',fontproperties=font_set)
plt.imshow(dst1)
plt.axis('off')
#逆时针旋转180