Python Pillow 简明教程

Python Pillow - Flip and Rotate Images

图像翻转和旋转是基本图像处理任务,通常用于增强可见性、纠正方向或实现特定效果。Python Pillow 函数库提供了简单的方法来对图像执行这些操作。

Flipping images

图像翻转是图像处理操作,它涉及沿指定轴翻转图像方向。此操作可以水平(即从左到右)或垂直(即从上到下)执行。在数字图像处理中,翻转通常用于多种目的,例如镜像、纠正相机方向或实现艺术效果。

在图像翻转的背景下,有两种主要类型,一种是水平翻转,另一种是垂直翻转。

  1. Horizontal Flip (Left to Right) − 在水平翻转中,每行像素都会反向,有效地从左到右镜像图像。

  2. Vertical Flip (Top to Bottom) − 在垂直翻转中,每列像素都会反向,有效地从上到下镜像图像。

可以通过使用 Pillow 库的 Image 模块的 transpose() 方法来实现这些操作。

Flipping images using the transpose() method

Pillow 中的 transpose() 方法用于操作图像的方向。它允许我们在图像上执行各种变换,例如通过指定我们想要应用的转置操作类型来进行旋转和镜像。

以下是 transpose() 方法的语法和参数。

PIL.Image.transpose(method)

其中,

  1. method − 要应用的转置方法。它可以采用以下值之一 − Image.Transpose.ROTATE_90 − 逆时针旋转图像 90 度。 Image.Transpose.ROTATE_180 − 旋转图像 180 度。 Image.Transpose.ROTATE_270 − 顺时针旋转图像 90 度。 Image.Transpose.FLIP_LEFT_RIGHT − 水平翻转图像,即从左到右。 Image.Transpose.FLIP_TOP_BOTTOM − 垂直翻转图像,即从上到下。

以下是本章所有示例中使用的输入图像。

python programming

Example

在此示例中,我们使用 transpose() 方法将输入图像逆时针旋转 90 度。

from PIL import Image

#Open an image
image = Image.open("Images/image_.jpg")

#Rotate the image 90 degrees counterclockwise
rotated_image = image.transpose(Image.Transpose.ROTATE_90)

#Save or display the rotated image
rotated_image.save("output Image/rotated_image.jpg")
open_rotated_image = Image.open("output Image/rotated_image.jpg")
open_rotated_image.show()
flipping

Example

在此示例中,我们使用 transpose() 方法水平翻转输入图像。

from PIL import Image

#Open an image
image = Image.open("Images/image_.jpg")

#Flip the image horizontally (left to right)
flipped_image = image.transpose(Image.Transpose.FLIP_LEFT_RIGHT)

#Save or display the rotated image
flipped_image.save("output Image/flipped_image.jpg")
open_flipped_image = Image.open("output Image/flipped_image.jpg")
open_flipped_image.show()
flipping horizontally

Example

在此示例中,我们使用 FLIP_TOP_BOTTOM 参数使用 transpose() 方法垂直翻转输入图像。

from PIL import Image

#Open an image
image = Image.open("Images/image_.jpg")

#Flip the image horizontally (left to right)
vflipped_image = image.transpose(Image.Transpose.FLIP_TOP_BOTTOM)

#Save or display the rotated image
vflipped_image.save("output Image/vflipped_image.jpg")
open_flipped_image = Image.open("output Image/vflipped_image.jpg")
open_flipped_image.show()
flipping right

Rotating Images

Pillow(Python Imaging Library)中的图像旋转是指通过特定角度更改图像方向的过程。这对于各种目的非常有用,例如纠正照片方向、创建特殊效果或对齐图像。

Pillow 提供了 rotate() 方法来执行此操作。以下是我们在 Pillow 中旋转图像时需要了解的要点。

  1. Rotate by Angle − rotate() 方法允许我们指定我们想要旋转图像的角度(以度为单位)。正角逆时针旋转图像,负角顺时针旋转图像。

  2. Canvas Expansion − 默认情况下,当我们旋转图像时,它可能不完全适合原始图像画布,这可能会导致裁剪。然后我们可以使用 expand 参数来确保旋转后的图像适合画布而不被裁剪。

Rotating images using the rotate() method

Image 模块中的 rotate() 方法用于执行给定输入图像的旋转。

以下是大致的 rotate() 方法的基本语法。

PIL.Image.rotate(angle, expand=False, resample=3)

其中,

  1. angle − 此参数指定旋转角度(以度为单位)。正值逆时针旋转图像,而负值顺时针旋转图像。

  2. expand (optional) − 如果将其设置为 True ,则允许扩展图像画布,以确保整个旋转后的图像都适合其中。如果将其设置为“False”(默认值),则会裁剪图像,以使其适合原始画布。

  3. resample (optional) − 可选的重采样滤波器。默认值为“3”,对应于抗锯齿高质量滤波器。我们可以从各种重采样滤波器中进行选择,例如“Image.NEAREST”、“Image.BOX”、“Image.BILINEAR”、“Image.HAMMING”、“Image.BICUBIC”、“Image.LANCZOS”等。

以下是本章所有示例中使用的输入图像。

flowers

Example

在此示例中,我们使用 rotate() 方法将给定的输入图像旋转 45 度。

from PIL import Image

#Open an image
image = Image.open("Images/flowers.jpg")

#Rotate the image by 45 degrees counterclockwise
rotated_image = image.rotate(45)

#Save the rotated image
rotated_image.save("output.jpg")
rotated_image.show()
rotate

Example

在此示例中,我们正在旋转输入图像,确保旋转后的图像适合画布而不裁剪。这可以通过将扩展参数设置为 True 来实现。

from PIL import Image

#Open an image
image = Image.open("Images/flowers.jpg")

#Rotate the image by 45 degrees and expand the canvas to fit the entire rotated image
rotated_image = image.rotate(45, expand=True)
rotated_image.show()
rotate flower

Example

在此示例中,我们通过将负值 -45 degrees 作为参数传递给 rotate() 方法,逆时针旋转图像。

from PIL import Image

#Open an image
image = Image.open("Images/flowers.jpg")

#Rotate the image by 45 degrees and expand the canvas to fit the entire rotated image
rotated_image = image.rotate(-45, expand=True)
rotated_image.show()
rotate anticlockwise