Convert EPUB to JPG in Python
EPUB is an open XML-based format for digital books and publications designed for optimal viewing on various devices. Converting EPUB files to other formats is often necessary to leverage different capabilities and features of those formats. JPG files can contain high-quality image data with lossless compression. This unique compression feature allows to quickly and efficiently share JPG images and use them widely on the Web, computers, and mobile devices. With Aspose.HTML for Python via .NET, you can convert EPUB to JPG format programmatically with full control over a wide range of conversion parameters.
In this article, you find information on how to convert EPUB to JPEG using convert_epub() methods of the Converter class and how to apply ImageSaveOptions.
Online EPUB Converter
You can convert EPUB to other formats with Aspose.HTML for Python via .NET API in real time. First, load an EPUB file from your local drive or URL and then run the example. The save options in the example are set by default. You will immediately receive the conversion result as a separate file.
If you want to convert EPUB to JPG using ImageSaveOptions
programmatically, please see the following Python code example.
Convert EPUB to JPG
To convert EPUB to JPG using ImageSaveOptions
, you should follow a few steps:
- Open an existing EPUB file. In the example, we use the open() method to open and read an EPUB file from the file system at the specified path.
- Create a new
ImageSaveOptions object with JPEG image format. By default, the
format
property is PNG. Here, you can set the required save options, such as page setup or resolution. - Use one of the
convert_epub() methods of the Converter class to convert EPUB to JPG. In the following example, you need to pass the EPUB file stream,
ImageSaveOptions
, and output file path to theconvert_epub()
method.
Look at the following Python code snippet that shows how to convert EPUB to JPG with custom save options using Aspose.HTML for Python via .NET.
1import os
2from aspose.html.converters import *
3from aspose.html.saving import *
4from aspose.html.drawing import *
5
6# Setup directories and define paths
7output_dir = "output/"
8input_dir = "data/"
9if not os.path.exists(output_dir):
10 os.makedirs(output_dir)
11document_path = os.path.join(input_dir, "input.epub")
12save_path = os.path.join(output_dir, "epub-to-image.jpeg")
13
14# Open an existing EPUB file for reading
15with open(document_path, "rb") as stream:
16
17 # Create an instance of ImageSaveOptions
18 options = ImageSaveOptions()
19 options.format.JPEG
20 options.horizontal_resolution = Resolution.from_dots_per_inch(200.0)
21 options.vertical_resolution = Resolution.from_dots_per_inch(200.0)
22 options.css.media_type.PRINT
23
24 # Convert EPUB to JPG
25 Converter.convert_epub(stream, options, save_path)
Save Options – ImageSaveOptions Class
The
ImageSaveOptions class in Aspose.HTML for Python via .NET offers extensive customization options for converting EPUB content to various image formats, ensuring high-quality output tailored to your needs. Here’s a more detailed description of each property of ImageSaveOptions
:
- The format property lets you specify the output image format, such as PNG, JPEG, BMP, GIF, or TIFF, with PNG as the default.
- The page_setup property enables precise control over the page layout, including size and margins, for optimal content placement.
- The background_color property sets the background color, with transparency as the default.
- The
css property, managed through
CssOptions
, dictates how CSS properties are handled during conversion. - The horizontal_resolution and vertical_resolution properties, defaulting to 300 dpi, define the pixel resolution per inch for internal and output images, enhancing image sharpness.
- The smoothing_mode property adjusts graphics rendering quality, which is crucial for achieving smooth, professional-grade images.
- The compression property specifies the compression method for TIFF files, supporting options like LZW, CCITT3, CCITT4, RLE, and NONE, which helps balance file size and image quality.
- The text property offers configurations for text rendering, ensuring clear and accurate text output in the converted images.
Download the Aspose.HTML for Python via .NET library to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.
Aspose.HTML offers a free online EPUB to JPG Converter that converts EPUB to JPG image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!