通過 Python 語言轉換圖像文件
轉換圖像、圖元文件和其他文件以構建 Python 圖像處理應用程序。
Aspose.Imaging for Python via .NET API 為程序員提供了不同的圖像處理功能。程序員可以在源代碼中使用它來將照片和圖片等光柵和矢量圖像轉換為 PSD、PDF、APNG、BMP、TIFF、GIF、PNG、DICOM、HTML5 CANVAS、WEBP、WMF、EMF、SVG、JPG、JPEG2000和其他圖像格式。 API 不僅包含轉換功能,還包含不同的過濾器、圖形處理、內存策略、裁剪、調整大小、調整和其他使用的操作。
如何將圖像轉換為 BMP、JPG、PNG
通過 .NET API 使用 Aspose.Imaging for Python,跨格式轉換很容易。這裡有幾個常見的例子,例如圖像到 bmp、圖像到 jpg、圖像到 png。過程是使用 Image.Load 加載圖像。使用指定設置創建所選 圖像格式選項 的對象。最後調用 Save Method 。
處理圖像轉換的Python代碼
from aspose.imaging import * | |
from aspose.imaging.fileformats.tiff.enums import * | |
from aspose.imaging.fileformats.jpeg2000 import * | |
from aspose.imaging.fileformats.png import * | |
from aspose.imaging.imageoptions import * | |
from aspose.pycore import is_assignable | |
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 | |
def process_convertion(): | |
import_formats, export_formats = get_available_image_formats() | |
for import_key, import_value in import_formats.items(): | |
format_ext = import_key | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
if not os.path.exists(input_file): | |
continue | |
for export_key, export_value in export_formats.items(): | |
output_file = os.path.join(templates_folder, f"convert-{format_ext}-to-{export_key}.{export_key}") | |
print("Processing conversion:" + output_file) | |
with Image.load(input_file) as image: | |
export_options = export_value.clone() | |
if is_assignable(image, VectorImage): | |
rasterization_options = import_value | |
rasterization_options.page_width = float(image.width) | |
rasterization_options.page_height = float(image.height) | |
export_options.vector_rasterization_options = rasterization_options | |
image.save(output_file, export_options) | |
if delete_output: | |
os.remove(output_file) | |
def get_available_image_formats(): | |
obj_init = Jpeg2000Options() | |
obj_init.codec = Jpeg2000Codec.J2K | |
obj_init2 = Jpeg2000Options() | |
obj_init2.codec = Jpeg2000Codec.JP2 | |
obj_init3 = PngOptions() | |
obj_init3.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
obj_init4 = {} | |
obj_init4["bmp"] = BmpOptions() | |
obj_init4["gif"] = GifOptions() | |
obj_init4["dicom"] = DicomOptions() | |
obj_init4["jpg"] = JpegOptions() | |
obj_init4["jpeg"] = JpegOptions() | |
obj_init4["jpeg2000"] = Jpeg2000Options() | |
obj_init4["j2k"] = obj_init | |
obj_init4["jp2"] = obj_init2 | |
obj_init4["png"] = obj_init3 | |
obj_init4["apng"] = ApngOptions() | |
obj_init4["tiff"] = TiffOptions(TiffExpectedFormat.DEFAULT) | |
obj_init4["tif"] = TiffOptions(TiffExpectedFormat.DEFAULT) | |
obj_init4["tga"] = TgaOptions() | |
obj_init4["webp"] = WebPOptions() | |
obj_init4["ico"] = IcoOptions(FileFormat.PNG, 24) | |
raster_formats_that_support_export_and_import = obj_init4 | |
obj_init5 = EmfOptions() | |
obj_init5.compress = True | |
obj_init6 = WmfOptions() | |
obj_init6.compress = True | |
obj_init7 = SvgOptions() | |
obj_init7.compress = True | |
obj_init8 = {} | |
obj_init8["emf"] = (EmfOptions(), EmfRasterizationOptions()) | |
obj_init8["svg"] = (SvgOptions(), SvgRasterizationOptions()) | |
obj_init8["wmf"] = (WmfOptions(), WmfRasterizationOptions()) | |
obj_init8["emz"] = (obj_init5, EmfRasterizationOptions()) | |
obj_init8["wmz"] = (obj_init6, WmfRasterizationOptions()) | |
obj_init8["svgz"] = (obj_init7, SvgRasterizationOptions()) | |
vector_formats_that_support_export_and_import = obj_init8 | |
obj_init9 = DxfOptions() | |
obj_init9.text_as_lines = True | |
obj_init9.convert_text_beziers = True | |
obj_init10 = {} | |
obj_init10["psd"] = PsdOptions() | |
obj_init10["dxf"] = obj_init9 | |
obj_init10["pdf"] = PdfOptions() | |
obj_init10["html"] = Html5CanvasOptions() | |
formats_only_for_export = obj_init10 | |
obj_init11 = {} | |
obj_init11["djvu"] = None | |
obj_init11["dng"] = None | |
obj_init11["dib"] = None | |
formats_only_for_import = obj_init11 | |
obj_init12 = {} | |
obj_init12["eps"] = EpsRasterizationOptions() | |
obj_init12["cdr"] = CdrRasterizationOptions() | |
obj_init12["cmx"] = CmxRasterizationOptions() | |
obj_init12["otg"] = OtgRasterizationOptions() | |
obj_init12["odg"] = OdgRasterizationOptions() | |
vector_formats_only_for_import = obj_init12 | |
# Get total set of formats to what we can export images | |
export_formats = {k: v[0] for k, v in vector_formats_that_support_export_and_import.items()} | |
export_formats.update(formats_only_for_export) | |
export_formats.update(raster_formats_that_support_export_and_import) | |
# Get total set of formats that can be loaded | |
import_formats = {k : VectorRasterizationOptions() for k in formats_only_for_import} | |
import_formats.update(vector_formats_only_for_import) | |
import_formats.update({k : v[1] for k, v in vector_formats_that_support_export_and_import.items()}) | |
return import_formats, export_formats | |
# run | |
process_convertion() |
如何將光柵圖像轉換為 PDF 文檔
將光柵圖像轉換為PDF的過程與圖像交叉轉換的過程相同,只是API為特定的PDF提供了 PdfOptions 設置。
將光柵圖像轉換為 PDF 的 Python 源代碼
from aspose.imaging import Image | |
from aspose.imaging.fileformats.pdf import PdfDocumentInfo | |
from aspose.imaging.imageoptions import PdfOptions | |
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 | |
output_file = os.path.join(data_dir, "output.pdf") | |
with Image.load(os.path.join(data_dir, "template.gif")) as image: | |
options = PdfOptions() | |
options.pdf_document_info = PdfDocumentInfo() | |
image.save(output_file, options); | |
# delete if necessary | |
if delete_output: | |
os.remove(output_file) |
如何將 SVG 圖像轉換為光柵 BMP、PNG、JPG
SVG 圖像的轉換是一樣的,加載 SVG 圖像,使用所需的圖像保存選項並調用 Save 方法。圖像 API 提供 SvgRasterizationOptions 用於設置 PageWidth、PageHeight 和光柵圖像使用其 VectorRasterizationOptions 屬性進行初始化和獲取 SvgRasterizationOptions 選項。
用於轉換 SVG 圖像光柵圖像的 Python 代碼
from aspose.imaging import Image | |
from aspose.imaging.fileformats.svg import SvgImage | |
from aspose.imaging.imageoptions import PngOptions, SvgRasterizationOptions | |
import os | |
import aspose.pycore as aspycore | |
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 | |
output_file = os.path.join(data_dir, "ConvertingSVGToRasterImages_out.png") | |
# Load the image and cast it as SvgImage | |
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.svg")), SvgImage) as image: | |
# Create an instance of relevant raster image options and Save the results to disk | |
png_options = PngOptions(); | |
svg_options = SvgRasterizationOptions(); | |
svg_options.page_width = 100.0; | |
svg_options.page_height = 200.0; | |
png_options.vector_rasterization_options = svg_options; | |
image.save(output_file, png_options); | |
# delete if necessary | |
if delete_output: | |
os.remove(output_file) |
使用 Python 語言轉換所有支持的圖像格式
下面是完整的圖像格式列表,您可以將其轉換為:
所有支持的圖像格式轉換為使用 Python 語言
下面是圖像格式的完整列表,您可以通過 .NET 使用 Aspose.Imaging for Python 轉換這些格式: