使用 Python 進行 SVG 影像合併
建立 Python 應用程式以透過伺服器 API 合併 SVG 圖像和照片
如何使用 Python 合併 SVG 影像和照片
利用拼貼技術,您可以從一組預先存在的圖像和照片中製作出迷人的作品。 Python 庫提供無縫合併影像和照片的功能,無論其來源檔案格式如何變更。突出您的作品集的有效策略是使用帶有重複圖像或圖案的壁紙裝飾,允許水平和垂直排列。如果您的目標是展示檔案處理的結果,那麼在套用影像效果之前和之後無縫合併兩個影像是一項簡單的任務。要合併 SVG 影像,我們將使用 Aspose.Imaging for Python via .NET API 是一個功能豐富、功能強大且易於使用的圖像處理和轉換 API,適用於 Python 平台。您可以使用系統命令中的以下命令安裝它。
系統命令行
>> pip install aspose-imaging-python-net
通過 Python 合併 SVG 的步驟
您需要 aspose-imaging-python-net 在您自己的環境中嘗試以下工作流程。
- 使用 Image.Load 方法加載 SVG 文件
- 將圖像組合成新圖像
- 以 Aspose.Imaging 支持的格式將合併後的圖像保存到光盤
系統要求
所有主要操作系統都支持 Python 的 Aspose.Imaging。只需確保您具有以下先決條件。
- 帶有 .NET Core 運行時的 Microsoft Windows / Linux。
- Python 和 PyPi 包管理器。
合併 SVG 圖像 - Python
from aspose.imaging import RasterImage, Image | |
from aspose.imaging.fileformats.tiff import TiffImage, TiffFrame | |
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.imaging.imageoptions import TiffOptions, PdfOptions, PngOptions | |
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 | |
data_dir = templates_folder | |
images = {} | |
files = ["template.emf", "template.wmf"] | |
max_width = 0 | |
max_height = 0 | |
i = 0 | |
for file_name in files: | |
with Image.load(os.path.join(data_dir, file_name)) as image: | |
output_file_name = os.path.join(data_dir, f"{file_name}{i}.png") | |
i += 1 | |
image.save(output_file_name, PngOptions()) | |
raster_image = Image.load(output_file_name) | |
if raster_image.width > max_width: | |
max_width = raster_image.width | |
if raster_image.height > max_height: | |
max_height = raster_image.height | |
images[output_file_name] = raster_image | |
output_path1 = os.path.join(data_dir, "multiframe.tiff") | |
output_path2 = os.path.join(data_dir, "multipage.pdf") | |
frame1 = next(iter(images.values())) | |
with TiffImage(TiffFrame(as_of(frame1, RasterImage))) as tiff_image: | |
# for loop | |
for path, page in images.items(): | |
if page != frame1: | |
tiff_image.add_page(as_of(page, RasterImage)) | |
tiff_image.save(output_path1, TiffOptions(TiffExpectedFormat.TIFF_JPEG_RGB)) | |
tiff_image.save(output_path2, PdfOptions()) | |
for path, image in images.items(): | |
# to dispose the image we call __exit__() | |
with image as _: | |
pass | |
if delete_output: | |
os.remove(path) | |
if delete_output: | |
os.remove(output_path1) | |
os.remove(output_path2) |
關於 Python API 的 Aspose.Imaging
Aspose.Imaging API 是一種圖像處理解決方案,用於在應用程序中創建、修改、繪製或轉換圖像(照片)。它提供:跨平台的圖像處理,包括但不限於各種圖像格式之間的轉換(包括統一的多頁或多幀圖像處理)、繪圖等修改、使用圖形基元、轉換(調整大小、裁剪、翻轉和旋轉) 、二值化、灰度、調整)、高級圖像處理功能(過濾、抖動、遮罩、去偏斜)和內存優化策略。它是一個獨立的庫,不依賴任何軟件進行圖像操作。可以在項目中使用原生 API 輕鬆添加高性能圖像轉換功能。這些是 100% 私有的本地 API,圖像在您的服務器上處理。通過在線應用合併 SVG
通過訪問我們的 Live Demos 網站 合併 SVG 文檔。 現場演示有以下好處
SVG 什麼是 SVG 文件格式
SVG 文件是使用基於 XML 的文本格式來描述圖像外觀的可縮放矢量圖形文件。 Scalable 這個詞指的是 SVG 可以縮放到不同的大小而不會損失任何質量。此類文件的基於文本的描述使它們獨立於分辨率。它是用於構建網站和打印圖形以實現可擴展性的最常用格式之一。該格式只能用於二維圖形。 SVG 文件可以在幾乎所有現代瀏覽器中查看/打開,包括 Chrome、Internet Explorer、Firefox 和 Safari。
閱讀更多其他支持的合併格式
使用 Python,可以輕鬆合併不同的格式,包括。