在 Python 中旋转图像

本教程介绍了使用 Python 旋转 image 的工作流程。它包括使用 Python 旋转照片的算法和工作代码示例。将此功能集成到任何操作系统(包括 macOS、Windows 和 Linux)上的应用程序中,并预先配置 Python。

在 Python 中旋转图像的步骤

  1. 通过配置 Aspose.Imaging 来旋转图像来设置环境
  2. 通过启动 Image 类的对象来读取源照片
  3. 将输入图像转换为 RasterImage 类的实例
  4. 设置旋转角度并旋转图像
  5. 导出旋转后的照片

这些步骤详细说明了在 Python 中更改图像角度的过程。读取源照片并将其旋转到所需的角度。最后,将输出图像保存到磁盘或流中以满足您的要求。

使用 Python 代码旋转照片

import aspose.imaging
from aspose.imaging import Image, RotateFlipType
import os
path = "C://SampleFiles//"
license = aspose.imaging.License()
license.set_license(path + "License.lic")
# Rotate, flip and save to the output file.
with Image.load(os.path.join(path, "grayscaled.jpg")) as image:
image.rotate_flip(RotateFlipType.ROTATE_90_FLIP_NONE)
image.save(os.path.join(path, f"rotate90.png"))
print("Image Rotated Successfully")

使用此代码片段在 Python 中将人像照片转换为风景照片。通过设置 RotateFlipType 枚举的各种其他值来进一步改进。例如,顺时针旋转 90 度、180 度或 270 度,并根据您的喜好垂直或水平翻转。

在本文中,您学习了使用 Python 旋转照片。此外,如果您想将图像转换为灰度,请阅读文章 在 Python 中将图像转换为灰度

 简体中文