使用 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,可以輕鬆裁剪不同的格式,包括。