使用 Python 进行 BMP 图像裁剪
创建 Python 应用程序以通过服务器 API 裁剪 BMP 图像和照片
如何使用 Python 裁剪 BMP 图像和照片
用户主要被位于图像或照片中心的元素所吸引。裁剪图像可能是消除可能分散照片主要主题注意力的不相关元素的有用步骤。利用专为Python设计的图像裁剪功能,您可以指示图像焦点周围的矩形区域,或从照片边缘建立清晰的边距以选择必要的部分。因此,通过裁剪图像,我们消除了任何不需要的细节并强调了主要主题。要以 BMP 格式裁剪图像,我们将使用 Aspose.Imaging for Python via .NET API 是一个功能丰富、功能强大且易于使用的图像处理和转换 API,适用于 Python 平台。您可以使用系统命令中的以下命令安装它。
系统命令行
>> pip install aspose-imaging-python-net
通过 Python 裁剪 BMP 的步骤
您需要 aspose-imaging-python-net 在您自己的环境中尝试以下工作流程。
- 使用 Image.Load 方法加载 BMP 文件 +裁剪图像;
- 以 Aspose.Imaging 支持的格式将合并后的图像保存到光盘
系统要求
所有主要操作系统都支持 Python 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 带有 .NET Core 运行时的 Microsoft Windows / Linux。
- Python 和 PyPi 包管理器。
裁剪 BMP 图像 - Python
from aspose.imaging import RasterImage, Image | |
from aspose.pycore import as_of | |
import os | |
if 'TEMPLATE_DIR' in os.environ: | |
templates_folder = os.environ['TEMPLATE_DIR'] | |
else: | |
templates_folder = r"C:\Users\USER\Downloads\templates" | |
delete_output = 'SAVE_OUTPUT' not in os.environ | |
obj_init = [] | |
obj_init.append("png") | |
obj_init.append("bmp") | |
obj_init.append("apng") | |
obj_init.append("dicom") | |
obj_init.append("jpg") | |
obj_init.append("jp2") | |
obj_init.append("j2k") | |
obj_init.append("tga") | |
obj_init.append("webp") | |
# obj_init.append("tiff") | |
obj_init.append("ico") | |
raster_formats = obj_init | |
for format_ext in raster_formats: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"cropped.{format_ext}") | |
print("Cropping ", format_ext) | |
# explicit type casting from Image to RasterImage | |
with as_of(Image.load(input_file), RasterImage) as image: | |
crop_width = image.width // 4 | |
crop_height = image.height // 4 | |
print("crop_width", crop_width, "crop_height", crop_height) | |
image.crop(crop_width, crop_width, crop_height, crop_height) | |
image.save(output_file) | |
if delete_output: | |
os.remove(output_file) |
关于 Python API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。通过在线应用裁剪 BMP
通过访问我们的 Live Demos 网站 裁剪 BMP 文档。 现场演示有以下好处
BMP 什么是 BMP 文件格式
扩展名为 .BMP 的文件表示用于存储位图数字图像的位图图像文件。这些图像独立于图形适配器,也称为设备独立位图 (DIB) 文件格式。这种独立性的目的是在 Microsoft Windows 和 Mac 等多个平台上打开文件。 BMP 文件格式可以将数据存储为单色和具有各种颜色深度的彩色格式的二维数字图像。
阅读更多其他支持的裁剪格式
使用 Python,可以轻松裁剪不同的格式,包括。