Manipulating EPS Images
Export EPS images to other formats
One of the popular vector image formats for combining text, graphics, and images is Encapsulated Postscript (EPS). EPS documents contain a PostScript program with page descriptions for publishing and may also include encapsulated preview images. Vector graphic editing applications have used the EPS format to save graphic documents since the 1980s, and it continues to be widely used.
With the Aspose.Imaging graphic library for Python, you can easily export EPS images to various file formats. To convert EPS images to other image formats, you simply load the EPS file into an `Image` object and save it in a different format, such as SVG, PNG, or PDF. Some image formats support various options for saving. In the case of the PNG image format, you can specify the color type as `PngColorType.GRAYSCALE`, as demonstrated in the Python code example below:
from aspose.imaging import Image | |
from aspose.imaging.imageoptions import PngOptions | |
from aspose.imaging.fileformats.png import PngColorType | |
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 | |
png_options = PngOptions() | |
png_options.color_type = PngColorType.GRAYSCALE | |
output_options = { | |
os.path.join(data_dir, "output.svg"): None, | |
os.path.join(data_dir, "output.png"): png_options | |
} | |
with Image.load(os.path.join(data_dir, "template.eps")) as image: | |
for key, options in output_options.items(): | |
if options is None: | |
image.save(key) | |
else: | |
image.save(key, options) | |
if delete_output: | |
for file in output_options: | |
os.remove(file) |
Resize EPS images before export
Moreover, you can perform additional operations on the image before exporting it to another image format. For instance, you can resize the image by increasing its width and height:
from aspose.imaging import Image | |
from aspose.imaging.imageoptions import PngOptions | |
from aspose.imaging.fileformats.png import PngColorType | |
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 | |
png_options = PngOptions() | |
png_options.color_type = PngColorType.GRAYSCALE | |
output_options = { | |
os.path.join(data_dir, "output.svg"): None, | |
os.path.join(data_dir, "output.png"): png_options | |
} | |
with Image.load(os.path.join(data_dir, "template.eps")) as image: | |
image.resize(image.width * 2, image.height * 2) | |
for key, options in output_options.items(): | |
if options is None: | |
image.save(key) | |
else: | |
image.save(key, options) | |
if delete_output: | |
for file in output_options: | |
os.remove(file) |
Extract preview image from the EPS files
Some EPS files may contain embedded preview images for display in various graphic applications. These preview images can be in different formats like TIFF bitmap, Windows Metafile (WMF), or JPEG. Supported formats are listed in the EpsPreviewFormat Enumeration. You can extract a preview image from the EPS file using the get_embedded_images() method and save it as a separate file:
import aspose.pycore as aspycore | |
from aspose.imaging import Image | |
from aspose.imaging.extensions import StreamExtensions as strm_ext | |
from aspose.imaging.fileformats.eps import EpsImage, EpsPreviewFormat | |
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 | |
tiffPreviewStream = None | |
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.eps")), EpsImage) as image: | |
tiffPreview = image.get_preview_image(EpsPreviewFormat.TIFF) | |
if tiffPreview is not None: | |
tiffPreviewStream = strm_ext.create_memory_stream() | |
tiffPreview.save(tiffPreviewStream) | |
# dispose memory allocated for tiffPreviewStream | |
if tiffPreviewStream is not None: | |
with tiffPreviewStream as _: | |
tiffPreviewStream.seek(0) | |
# do something with the stream tiffPreviewStream | |
# ... |