Convert EPUB to PNG in Python
PNG file format supports lossless image compression that makes it popular among its users. It widely takes to transmit pictures over the network, display photos and graphics on web pages and reserve in cloud storages. With Aspose.HTML for Python via .NET, you can convert EPUB to PNG format programmatically with full control over a wide range of conversion parameters.
In this article, you find information on how to convert EPUB to PNG using convert_epub() methods of the Converter class and how to apply ImageSaveOptions.
To continue following this tutorial, install and configure Aspose.HTML for Python via .NET in your Python project.
Online EPUB Converter
You can test the Aspose.HTML for Python via .NET API functionality and convert EPUB in real-time. Please load an EPUB file from a local file system or URL, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.
If you want to convert EPUB to PNG using ImageSaveOptions
programmatically, please see the following Python code example.
Convert EPUB to PNG
Using
convert_epub() methods is the most common way to convert EPUB files into various formats. To convert EPUB to PNG with ImageSaveOptions
specifying, 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. 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 save EPUB as a PNG image.
Look at the following Python code snippet that shows how to convert EPUB to PNG with custom save options using Aspose.HTML for Python via .NET.
1import os
2from aspose.html import *
3from aspose.html.converters import *
4from aspose.html.saving import *
5from aspose.html.drawing import *
6from aspose.pydrawing import *
7
8# Setup directories and define paths
9output_dir = "output/"
10input_dir = "data/"
11if not os.path.exists(output_dir):
12 os.makedirs(output_dir)
13document_path = os.path.join(input_dir, "input.epub")
14save_path = os.path.join(output_dir, "epub-to-image.png")
15
16# Open an existing EPUB file for reading
17with open(document_path, "rb") as stream:
18
19 # Create an instance of ImageSaveOptions
20 options = ImageSaveOptions()
21 options.css.media_type.SCREEN
22 options.text.text_rendering_hint = text.TextRenderingHint.CLEAR_TYPE_GRID_FIT
23
24 # Convert EPUB to PNG
25 Converter.convert_epub(stream, options, save_path)
Save Options – ImageSaveOptions Class
The PNG images creation functionality can be enhanced with save options per your needs. Aspose.HTML for Python via .NET allows converting HTML to PNG using default or custom save options. The
ImageSaveOptions class offers extensive customization for converting EPUB content into image formats. Here’s a more detailed description of each property of ImageSaveOptions
:
- format – This property determines the format of the output image. The supported formats include common image formats like PNG, JPEG, BMP, GIF, and TIFF. The default format is PNG, but you can specify others based on your requirements.
- page_setup – You can configure the page layout settings for the output image. This includes specifying the page size and margins (top, bottom, left, right) to control the placement and display of HTML content within the image.
- background_color – This property allows you to set the background color for the rendered output. If not set, the default background is transparent.
- css – This property, represented by
CssOptions
, allows configuring how CSS properties are processed during the HTML to image conversion. - horizontal_resolution – This property sets or gets the horizontal resolution (in pixels per inch) for both output and internal images used during processing. Higher resolution usually results in a sharper image but can also increase file size. By default, it is 300 dpi.
- vertical_resolution – This property sets or gets the vertical resolution for internal images in pixels per inch. By default, it is 300 dpi.
- smoothing_mode – This property controls the quality of graphics rendering during conversion. It affects how images are rendered, which is especially useful for anti-aliasing and achieving smooth and visually appealing output. Options typically include settings for high-quality rendering, which can be critical for professional and presentation-grade images.
- compression – The compression option allows you to set the compression method for TIFF output. Supported options: LZW, CCITT3, CCITT4, RLE, and NONE. Compression helps reduce file size while maintaining image quality, which is especially important for TIFF files used for high-quality archiving and printing of images.
- text – This property provides configurations for text rendering during HTML to image conversion.
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 PNG Converter that converts EPUB to PNG image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!