Use Python for EPS Images Compression
Create Python Apps to Compress EPS Images and Photos via Server APIs
How to Compress EPS Images and Photos with Python
The adapting of images for publishing includes not only creative processes but also technical adjustments, such as file compression. Generally, high-resolution images are necessary for printed materials or outdoor advertising, whereas websites may face challenges with large file sizes. Image compression settings can vary depending on their intended use and where they will be published. Large files can take a considerable amount of time to download, especially on mobile connections, impacting the overall user experience. However, overly compressed images might suffer from blurriness and visible pixelation, undermining the visual quality. Achieving a balance between file size and image quality requires a careful selection of algorithms and compression levels. To compress images in EPS format, we’ll employ Aspose.Imaging for Python via .NET API which is a feature-rich, powerful and easy to use image manipulation and conversion API for Python platform. You may install it using the following command from your system command.
The system command line
>> pip install aspose-imaging-python-net
Steps to Compress EPS via Python
You need the aspose-imaging-python-net to try the following workflow in your own environment.
- load EPS files with Image.Load method;
- compress images;
- save compressed image to disc in the supported by Aspose.Imaging format.
System Requirements
Aspose.Imaging for Python is supported on all major operating systems. Just make sure that you have the following prerequisites.
- Microsoft Windows / Linux with .NET Core Runtime.
- Python and PyPi package manager.
Compress EPS images - Python
from aspose.imaging.imageoptions import * | |
from aspose.imaging import * | |
from aspose.pycore import as_of, 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 | |
def compress_vector_format_to_svg(): | |
obj_init = [] | |
obj_init.append("cdr") | |
obj_init.append("cmx") | |
obj_init.append("odg") | |
obj_init.append("otg") | |
obj_init.append("eps") | |
format_exts = obj_init | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init2 = CdrRasterizationOptions() | |
obj_init2.page_width = float(image.width) | |
obj_init2.page_height = float(image.height) | |
return obj_init2 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init3 = CmxRasterizationOptions() | |
obj_init3.page_width = float(image.width) | |
obj_init3.page_height = float(image.height) | |
return obj_init3 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init4 = OdgRasterizationOptions() | |
obj_init4.page_width = float(image.width) | |
obj_init4.page_height = float(image.height) | |
return obj_init4 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init5 = OtgRasterizationOptions() | |
obj_init5.page_width = float(image.width) | |
obj_init5.page_height = float(image.height) | |
return obj_init5 | |
else: | |
return None | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init6 = SvgOptions() | |
obj_init6.compress = True | |
obj_init6.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.svgz" | |
page.save(out_file, obj_init6) | |
if delete_output: | |
os.remove(out_file) | |
page_index += 1 | |
else: | |
obj_init7 = SvgOptions() | |
obj_init7.compress = True | |
obj_init7.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".svgz", obj_init7) | |
if delete_output: | |
os.remove(output_file + ".svgz") | |
def compress_vector_format_to_wmf(): | |
obj_init8 = [] | |
obj_init8.append("cdr") | |
obj_init8.append("cmx") | |
obj_init8.append("odg") | |
obj_init8.append("otg") | |
obj_init8.append("eps") | |
format_exts = obj_init8 | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init9 = CdrRasterizationOptions() | |
obj_init9.page_width = float(image.width) | |
obj_init9.page_height = float(image.height) | |
return obj_init9 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init10 = CmxRasterizationOptions() | |
obj_init10.page_width = float(image.width) | |
obj_init10.page_height = float(image.height) | |
return obj_init10 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init11 = OdgRasterizationOptions() | |
obj_init11.page_width = float(image.width) | |
obj_init11.page_height = float(image.height) | |
return obj_init11 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init12 = OtgRasterizationOptions() | |
obj_init12.page_width = float(image.width) | |
obj_init12.page_height = float(image.height) | |
return obj_init12 | |
else: | |
return None | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init13 = WmfOptions() | |
obj_init13.compress = True | |
obj_init13.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.wmz" | |
page.save(out_file, obj_init13) | |
if delete_output: | |
os.remove(out_file) | |
else: | |
obj_init14 = WmfOptions() | |
obj_init14.compress = True | |
obj_init14.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".wmz", obj_init14) | |
if delete_output: | |
os.remove(output_file + ".wmz") | |
def compress_vector_formats_to_emf(): | |
obj_init15 = [] | |
obj_init15.append("cdr") | |
obj_init15.append("cmx") | |
obj_init15.append("odg") | |
obj_init15.append("otg") | |
obj_init15.append("eps") | |
format_exts = obj_init15 | |
for format_ext in format_exts: | |
input_file = os.path.join(templates_folder, f"template.{format_ext}") | |
output_file = os.path.join(templates_folder, f"compressed_{format_ext.upper()}") | |
with Image.load(input_file) as image: | |
def rasterization_options_factory(): | |
tmp_switch = image.file_format | |
if tmp_switch == FileFormat.CDR: | |
obj_init16 = CdrRasterizationOptions() | |
obj_init16.page_width = float(image.width) | |
obj_init16.page_height = float(image.height) | |
return obj_init16 | |
elif tmp_switch == FileFormat.CMX: | |
obj_init17 = CmxRasterizationOptions() | |
obj_init17.page_width = float(image.width) | |
obj_init17.page_height = float(image.height) | |
return obj_init17 | |
elif tmp_switch == FileFormat.ODG: | |
obj_init18 = OdgRasterizationOptions() | |
obj_init18.page_width = float(image.width) | |
obj_init18.page_height = float(image.height) | |
return obj_init18 | |
elif tmp_switch == FileFormat.OTG: | |
obj_init19 = OtgRasterizationOptions() | |
obj_init19.page_width = float(image.width) | |
obj_init19.page_height = float(image.height) | |
return obj_init19 | |
else: | |
return None | |
multi_page = None | |
# if image implements an IMultipageImage interface | |
if is_assignable(image, IMultipageImage): | |
multi_page = as_of(image, IMultipageImage) | |
if multi_page is not None and multi_page.page_count > 1: | |
# for loop | |
for_first_step2 = True | |
page_index = 0 | |
for page in multi_page.pages: | |
obj_init20 = EmfOptions() | |
obj_init20.compress = True | |
obj_init20.vector_rasterization_options = rasterization_options_factory() | |
out_file = f"{output_file}_p{page_index + 1}.emz" | |
page.save(out_file, obj_init20) | |
if delete_output: | |
os.remove(out_file) | |
else: | |
obj_init21 = EmfOptions() | |
obj_init21.compress = True | |
obj_init21.vector_rasterization_options = rasterization_options_factory() | |
image.save(output_file + ".emz", obj_init21) | |
if delete_output: | |
os.remove(output_file + ".emz") | |
# run | |
compress_vector_formats_to_emf() | |
compress_vector_format_to_svg() | |
compress_vector_format_to_wmf() |
About Aspose.Imaging for Python API
Aspose.Imaging API is an image processing solution to create, modify, draw or convert images (photos) within applications. It offers: cross-platform Image processing, including but not limited to conversions between various image formats (including uniform multi-page or multi-frame image processing), modifications such as drawing, working with graphic primitives, transformations (resize, crop, flip&rotate, binarization, grayscale, adjust), advanced image manipulation features (filtering, dithering, masking, deskewing), and memory optimization strategies. It’s a standalone library and does not depend on any software for image operations. One can easily add high-performance image conversion features with native APIs within projects. These are 100% private on-premise APIs and images are processed at your servers.Compress EPS via Online App
Compress EPS documents by visiting our Live Demos website The live demo has the following benefits
EPS What is EPS File Format
FIles with EPS extension essentially describe an Encapsulated PostScript language program that describes the appearance of a single page. The name "Encapsulated" because it can be included or encapsulated in another PostScript language page description. This script based file format may contain any combination of text, graphics and images. EPS files may include a bitmap preview image encapsulated inside for display by applications that can open such files. EPS files can be converted to standard image formats such as JPG, PNG, TIFF and PDF using different applications e.g. Adobe Illustrator, Photoshop and PaintShop Pro. Because of a security vulnerability in EPS files, Office 2016, Office 2013, Office 2010, and Office 365 have turned off the ability to insert EPS files into Office documents.
Read MoreOther Supported Compress Formats
Using Python, one can easily compress different formats including: