使用 Python 進行 WEBP 影像合併
建立 Python 應用程式以透過伺服器 API 合併 WEBP 圖像和照片
如何使用 Python 合併 WEBP 影像和照片
利用拼貼技術,您可以從一組預先存在的圖像和照片中製作出迷人的作品。 Python 庫提供無縫合併影像和照片的功能,無論其來源檔案格式如何變更。突出您的作品集的有效策略是使用帶有重複圖像或圖案的壁紙裝飾,允許水平和垂直排列。如果您的目標是展示檔案處理的結果,那麼在套用影像效果之前和之後無縫合併兩個影像是一項簡單的任務。要合併 WEBP 影像,我們將使用 Aspose.Imaging for Python via .NET API 是一個功能豐富、功能強大且易於使用的圖像處理和轉換 API,適用於 Python 平台。您可以使用系統命令中的以下命令安裝它。
系統命令行
>> pip install aspose-imaging-python-net
通過 Python 合併 WEBP 的步驟
您需要 aspose-imaging-python-net 在您自己的環境中嘗試以下工作流程。
- 使用 Image.Load 方法加載 WEBP 文件
- 將圖像組合成新圖像
- 以 Aspose.Imaging 支持的格式將合併後的圖像保存到光盤
系統要求
所有主要操作系統都支持 Python 的 Aspose.Imaging。只需確保您具有以下先決條件。
- 帶有 .NET Core 運行時的 Microsoft Windows / Linux。
- Python 和 PyPi 包管理器。
合併 WEBP 圖像 - Python
from aspose.imaging import Image, Graphics, Color, Rectangle | |
from aspose.imaging.fileformats.png import PngColorType | |
from aspose.imaging.imageoptions import PngOptions | |
from aspose.imaging.sources import StreamSource | |
import os | |
import tempfile | |
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 | |
data_dir = templates_folder | |
images = [] | |
files = ["template.png", "template.jpg"] | |
merge_direction = [0, 1] | |
max_width = 0 | |
max_height = 0 | |
total_width = 0 | |
total_height = 0 | |
for file_name in files: | |
image = Image.load(os.path.join(data_dir, file_name)) | |
total_width += image.width | |
if image.width > max_width: | |
max_width = image.width | |
total_height += image.height | |
if image.height > max_height: | |
max_height = image.height | |
images.append(image) | |
def get_temp_file_name(): | |
f = tempfile.NamedTemporaryFile() | |
file_name = f.name | |
f.close() | |
return file_name | |
def merge_images(direction): | |
target_width = 0 | |
target_height = 0 | |
if direction == 0: | |
target_width = total_width | |
target_height = max_height | |
else: | |
target_width = max_width | |
target_height = total_height | |
output_path = data_dir | |
output_path = os.path.join(output_path, "result" + str(direction) + ".png") | |
png_options = PngOptions() | |
png_options.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
tmp_file = get_temp_file_name() | |
with open(tmp_file, "wb") as stream: | |
png_options.source = StreamSource(stream) | |
with Image.create(png_options, target_width, target_height) as image: | |
image.background_color = Color.white | |
graphics = Graphics(image) | |
x = 0 | |
y = 0 | |
graphics.begin_update() | |
for frame in images: | |
print("x", x, "y", y) | |
graphics.draw_image(frame, Rectangle(x, y, frame.width, frame.height)) | |
if direction == 0: | |
x += frame.width | |
if direction == 1: | |
y += frame.height | |
graphics.end_update() | |
image.save(output_path) | |
os.remove(tmp_file) | |
if delete_output: | |
os.remove(output_path) | |
# run | |
merge_images(0) | |
merge_images(1) | |
for image in images: | |
# to dispose the image we call __exit__() | |
with image as _: | |
pass |
關於 Python API 的 Aspose.Imaging
Aspose.Imaging API 是一種圖像處理解決方案,用於在應用程序中創建、修改、繪製或轉換圖像(照片)。它提供:跨平台的圖像處理,包括但不限於各種圖像格式之間的轉換(包括統一的多頁或多幀圖像處理)、繪圖等修改、使用圖形基元、轉換(調整大小、裁剪、翻轉和旋轉) 、二值化、灰度、調整)、高級圖像處理功能(過濾、抖動、遮罩、去偏斜)和內存優化策略。它是一個獨立的庫,不依賴任何軟件進行圖像操作。可以在項目中使用原生 API 輕鬆添加高性能圖像轉換功能。這些是 100% 私有的本地 API,圖像在您的服務器上處理。通過在線應用合併 WEBP
通過訪問我們的 Live Demos 網站 合併 WEBP 文檔。 現場演示有以下好處
WEBP 什麼是 WEBP 文件格式
WebP 是 Google 推出的一種現代光柵 Web 圖像文件格式,它基於無損和有損壓縮。它提供相同的圖像質量,同時大大減小了圖像尺寸。由於大多數網頁使用圖像作為數據的有效表示,因此在網頁中使用 WebP 圖像會導致網頁加載速度更快。根據谷歌的說法,WebP 無損圖像的大小比 PNG 小 26%,而 WebP 有損圖像比同類 JPEG 圖像小 25-34%。基於 WebP 和其他圖像文件格式之間的結構相似性 (SSIM) 索引比較圖像。 WebP 是 WebM 多媒體容器格式的姊妹項目。
閱讀更多其他支持的合併格式
使用 Python,可以輕鬆合併不同的格式,包括。