平移变换

用image对象的rotate方法对图像进行平移变换,该方法的语法格式为:

code.python
img.rotate(angle,resample,expand,center,translate,fillcolor)

进行平移变换,设置其中的translate参数。该参数的取值为一个2元素向量 (h,v), h和v分别表示水平方向和垂向上的平移量。注意,左上角为原点,X轴向右为正,Y轴向下为正。

下面的代码打开一个图像,用image对象的rotate方法对图像进行平移,水平向右平移100个像素,垂直向上平移20个像素。

code.python
>>> from PIL import Image
>>> img=Image.open('D:\\pic.jpg')
>>> 
>>> img=img.rotate(0,translate=(100,-20))
>>> img.show()

平移结果如下图所示。

Document Image