Open PSD, PSB and AI files and export them to PDF, PNG, TIFF, GIF, BMP, JPEG
Overview
To convert PSD, PSB, and AI files to different formats, you can use the Aspose.PSD library in Python. This library provides various options and settings to customize the conversion process.
First, you need to import the necessary classes and modules from the Aspose.PSD library. Make sure you have installed the library before running the code.
In this code, we define various options for different formats, such as PNG, PDF, TIFF, JPEG, BMP, JPEG2000, GIF, PSB, and PSD. These options allow you to customize the output file according to your requirements.
Next, we define a dictionary formats that maps file extensions to their respective save options.
To convert a PSD file to other formats, you need to load the PSD file using PsdImage.load() and then iterate through the formats dictionary. For each format, specify the output file name and save the image using the image.save() method.
Similarly, to convert an AI file to other formats, load the AI file using AiImage.load() and iterate through the formats dictionary. Specify the output file name and save the image using the image.save() method.
Make sure to provide the correct file paths for the source PSD and AI files.
That’s it! You can now use this code to convert PSD, PSB, and AI files to various formats using Aspose.PSD for Python.
Please check full example.
Example
from aspose.psd.fileformats.ai import AiImage | |
from aspose.psd.fileformats.png import PngColorType | |
from aspose.psd.fileformats.psd import PsdImage | |
from aspose.psd.fileformats.tiff.enums import TiffExpectedFormat | |
from aspose.psd.imageoptions import PngOptions, PdfOptions, TiffOptions, JpegOptions, BmpOptions, Jpeg2000Options, \ | |
GifOptions, PsdOptions | |
def ExportPsdAndAIToDifferentFormatsTest(self): | |
# Saving to PNG | |
pngSaveOpt = PngOptions() | |
pngSaveOpt.color_type = PngColorType.TRUECOLOR_WITH_ALPHA | |
# Saving to PDF | |
pdfSaveOpt = PdfOptions() | |
# Saving to Tiff | |
tiffSaveOpt = TiffOptions(TiffExpectedFormat.TIFF_NO_COMPRESSION_RGBA) | |
# Saving to Jpeg | |
jpegSaveOpt = JpegOptions() | |
jpegSaveOpt.quality = 90 | |
# Saving to BMP | |
bmpSaveOpt = BmpOptions() | |
# Saving to JPEG2000 | |
j2kSaveOpt = Jpeg2000Options() | |
# Saving to GIF | |
gifSaveOpt = GifOptions() | |
# Saving to PSB | |
psbSaveOpt = PsdOptions() | |
psbSaveOpt.version = 2 | |
# Saving to PSD | |
psdSaveOpt = PsdOptions() | |
formats = { | |
"pdf": pdfSaveOpt, | |
"jpg": jpegSaveOpt, | |
"png": pngSaveOpt, | |
"tif": tiffSaveOpt, | |
"gif": gifSaveOpt, | |
"j2k": j2kSaveOpt, | |
"bmp": bmpSaveOpt, | |
"psb": psbSaveOpt, | |
"psd": psdSaveOpt | |
} | |
# Saving PSD to other formats | |
sourcePsd = "AllTypesLayerPsd2.psd" | |
with PsdImage.load(sourcePsd) as image: | |
for format, saveOpt in formats.items(): | |
fn = "export.psd.to." + format | |
image.save(fn, saveOpt) | |
# Saving AI to other formats | |
sourceAi = "ai_one_text_3.ai" | |
with AiImage.load(sourceAi) as image: | |
for format, saveOpt in formats.items(): | |
fn = "export.ai.to." + format | |
image.save(fn, saveOpt) |