Manipulating Photoshop Formats
Exporting Image to PSD
PSD, PhotoShop document, is the default file format used by Adobe PhotoShop for working with images. Aspose.Imaging lets you save files to PSD so that they can be opened and edited in PhotoShop. This article shows how to save a file to PSD with Aspose.Imaging, and it also discusses some of the settings that can be used when saving to this format. PsdOptions is a specialized class in the ImageOptions namespace used to export images to PSD. To export to PSD, create an instance of the Image class, either loaded from an existing image file or created from scratch. This article explains how. In the examples below, an existing image is loaded by passing the file path to the Image class static Load method. Once it is loaded, save the image using the Image class Save method, and supply a PsdOptions object as the second argument. Several of the PsdOptions class properties can be set for advanced conversion. Some of the properties are ColorMode, CompressionMethod and Version. Aspose.Imaging for Python via .NET supports the following compression methods through the CompressionMethod enumeration:
- CompressionMethod.RAW
- CompressionMethod.RLE
- CompressionMethod.ZIP_WITHOUT_PREDICTION
- CompressionMethod.ZIP_WITH_PREDICTION
The following color modes are supported through the ColorModes enumeration:
- ColorModes.BITMAP
- ColorModes.GRAYSCALE
- ColorModes.RGB
Additional resources can be added, such as thumbnail resources for PSD v4.0, v5.0 and higher, or grid and guide resources for PSD v4.0 and higher. The code below opens a BMP file from disk and saves it to PSD with RLE compression and grayscale color mode, the following code snippet shows you how to export Image to PSD.
Creating Indexed PSD Files
Aspose.Imaging for Python via .NET API can create Indexed PSD files from scratch. This article demonstrates the use of PsdOptions and PsdImage classes to create an Indexed PSD while drawing some shapes over the newly created canvas. The following simple steps are required to create an Indexed PSD file.
- Create an instance of PsdOptions and set it’s source.
- Set ColorMode property of the PsdOptions to ColorModes.INDEXED.
- Create a new palette of colors from RGB space and set it as palette property of PsdOptions.
- Set CompressionMethod property to required compression algorithm.
- Create a new PSD image by calling the PsdImage.create method.
- Draw graphics or perform other operations as per requirement.
- Call PsdImage.save method to commit all changes.
The following code snippet shows you how to create indexed PSD files.
import aspose.pycore as aspycore | |
from aspose.imaging import Image, Color, Graphics, Rectangle, ColorPalette, Pen | |
from aspose.imaging.fileformats.psd import ColorModes, CompressionMethod | |
from aspose.imaging.imageoptions import PsdOptions | |
from aspose.imaging.sources import FileCreateSource | |
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 | |
with PsdOptions() as create_options: | |
create_options.source = FileCreateSource(os.path.join(data_dir, "Newsample_out.psd"), False) | |
create_options.color_mode = ColorModes.INDEXED | |
create_options.version = 5 | |
# Create a new color palette having RGB colors, Set Palette property & compression method | |
palette = [Color.red, Color.white] | |
create_options.palette = ColorPalette.create_with_colors(palette) | |
create_options.compression_method = CompressionMethod.RLE | |
# Create a new PSD with PsdOptions created previously | |
with Image.create(create_options, 500, 500) as psd: | |
# Draw some graphics over the newly created PSD | |
graphics = Graphics(psd) | |
graphics.clear(Color.white) | |
graphics.draw_ellipse(Pen(Color.red, 6), Rectangle(0, 0, 400, 400)) | |
psd.save() | |
if delete_output: | |
os.remove(os.path.join(data_dir, "Newsample_out.psd")) |
Support for EPS Format
This article shows how to option parsing and rendering for PSD text layer. The following code snippet shows you how to support SmallCap.
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.fileformats.eps import EpsImage, EpsInterchangeImage, EpsBinaryImage | |
from aspose.imaging.fileformats.eps.consts import EpsType | |
from aspose.imaging.imageoptions import PngOptions | |
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 | |
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.eps")), EpsImage) as eps_image: | |
# check if EPS image has any raster preview to proceed (for now only raster preview is supported) | |
if eps_image.has_raster_preview: | |
if eps_image.photoshop_thumbnail is not None: | |
# process Photoshop thumbnail if it's present | |
pass | |
if eps_image.eps_type == EpsType.INTERCHANGE: | |
# Get EPS Interchange subformat instance | |
eps_interchange_image = aspycore.as_of(eps_image, EpsInterchangeImage) | |
if eps_interchange_image.raster_preview is not None: | |
# process black-and-white Interchange raster preview if it's present | |
pass | |
else: | |
# Get EPS Binary subformat instance | |
eps_binary_image = aspycore.as_of(eps_image, EpsBinaryImage) | |
if eps_binary_image.tiff_preview is not None: | |
# process TIFF preview if it's present | |
pass | |
if eps_binary_image.wmf_preview is not None: | |
# process WMF preview if it's present | |
pass | |
# export EPS image to PNG (by default, best available quality preview is used for export) | |
eps_image.save(os.path.join(data_dir, "anyEpsFile.png"), PngOptions()) | |
if delete_output: | |
os.remove(os.path.join(data_dir, "anyEpsFile.png")) |