Manipulating DjVu Formats

Converting DjVu to TIFF Format

Aspose.Imaging APIs are capable of loading DjVu files for possible conversion to raster image formats. This example demonstrates the usage of Aspose.Imaging for Python via .NET API to convert a DjVu file having multiple pages to a multipage TIFF image. You can convert a DjVu file having multiple pages to TIFF image as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of TiffOptions while using any of the TiffExpectedFormat enumeration fields.
  3. Create an instance of DjvuMultiPageOptions and set it as MultiPageOptions property of the TiffOptions created in previous step.
  4. Call Image.save by passing the file path as the instance of TiffOptions.
import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.djvu import DjvuImage
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat
from aspose.imaging.imageoptions import TiffOptions, DjvuMultiPageOptions
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
# Load a DjVu image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu")), DjvuImage) as image:
# Create an instance of TiffOptions & use preset options for Black n While with Deflate compression
export_options = TiffOptions(TiffExpectedFormat.TIFF_DEFLATE_BW)
# Initialize the DjvuMultiPageOptions and Call Save method while passing instance of TiffOptions
export_options.multi_page_options = DjvuMultiPageOptions()
image.save(os.path.join(data_dir, "result.tiff"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.tiff"))

Converting Range of DjVu Pages

A DjVu image may have more than one pages like a multipage TIFF or multiframe GIF. If such DjVu image has to be converted to any raster format that could have more than one page or frame then be default, all pages of DjVu are exported. Aspose.Imaging APIs also provides the facility to convert only specific range of DjVu pages to multipage TIFF or multiframe GIF. You can convert a specific range of DjVu pages to multiframe GIF or TIFF using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of TiffOptions or GifOptions.
  3. Create an instance of IntRange and initialize it with range of pages to be exported.
  4. Create an instance of DjvuMultiPageOptions with instance of IntRange as parameter and set it as MultiPageOptions property of the ImageOptionsBase created in step 2.
  5. Call Image.Save by passing the file path as well as the instance of ImageOptionsBase.

Here is the source code to convert first 2 pages of DjVu to TIFF format.

import aspose.pycore as aspycore
from aspose.imaging import Image, IntRange
from aspose.imaging.fileformats.djvu import DjvuImage
from aspose.imaging.fileformats.tiff.enums import TiffExpectedFormat
from aspose.imaging.imageoptions import TiffOptions, DjvuMultiPageOptions
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
# Load a DjVu image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu")), DjvuImage) as image:
# Create an instance of TiffOptions & use preset options for Black n While with Deflate compression
export_options = TiffOptions(TiffExpectedFormat.TIFF_DEFLATE_BW)
range_ = IntRange(0, 1)
# Initialize an instance of DjvuMultiPageOptions while passing instance of IntRange and Call Save method while passing instance of TiffOptions
export_options.multi_page_options = DjvuMultiPageOptions(range_)
image.save(os.path.join(data_dir, "result.tiff"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.tiff"))

Following code snippet converts the first 2 pages of DjVu to GIF format.

import aspose.pycore as aspycore
from aspose.imaging import Image, IntRange, Rectangle
from aspose.imaging.fileformats.png import PngColorType
from aspose.imaging.fileformats.djvu import DjvuImage
from aspose.imaging.imageoptions import PngOptions, DjvuMultiPageOptions
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
# Load a DjVu image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu")), DjvuImage) as image:
# Create an instance of PngOptions and Set ColorType to Grayscale
export_options = PngOptions()
export_options.color_type = PngColorType.GRAYSCALE
# Create an instance of Rectangle and specify the portion on DjVu page
export_area = Rectangle(0, 0, 500, 500)
# Specify the DjVu page index and Initialize an instance of DjvuMultiPageOptions while passing index of DjVu page index and instance of Rectangle covering the area to be exported
export_page_index = 0
export_options.multi_page_options = DjvuMultiPageOptions(export_page_index, export_area)
image.save(os.path.join(data_dir, "result.png"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Converting Range of DjVu Pages to Separate Images

It is possible to convert only a specific range of DjVu pages to multipage TIFF or multiframe GIF where the resultant image will contain multiple pages/frames. Aspose.Imaging APIs also provide the means to convert the specific range of DjVu pages to raster image formats that do not support layering such as BMP, PNG & JPEG. In this case each DjVu page will be converted to a separate image. You can convert a specific range of DjVu pages to separate images using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of ImageOptionsBase.
  3. Create an instance of IntRange and initialize it with range of pages to be exported.
  4. Create an instance of DjvuMultiPageOptions while passing the indices of DjVu pages as parameter and set it as MultiPageOptions property of the ImageOptionsBase created in step 2.
  5. Call Image.save by passing the file path as well as the instance of ImageOptionsBase.

Here is the source code to convert first 2 pages of DjVu to BMP format.

import aspose.pycore as aspycore
from aspose.imaging import Image, IntRange, Rectangle
from aspose.imaging.fileformats.png import PngColorType
from aspose.imaging.fileformats.djvu import DjvuImage
from aspose.imaging.imageoptions import PngOptions, DjvuMultiPageOptions
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
# Load a DjVu image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu")), DjvuImage) as image:
# Create an instance of PngOptions and Set ColorType to Grayscale
export_options = PngOptions()
export_options.color_type = PngColorType.GRAYSCALE
# Create an instance of Rectangle and specify the portion on DjVu page
export_area = Rectangle(0, 0, 500, 500)
# Specify the DjVu page index and Initialize an instance of DjvuMultiPageOptions while passing index of DjVu page index and instance of Rectangle covering the area to be exported
export_page_index = 0
export_options.multi_page_options = DjvuMultiPageOptions(export_page_index, export_area)
image.save(os.path.join(data_dir, "result.png"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Converting Specific Portion of DjVu Page

Aspose.Imaging APIs provides an easy to use mechanism to export only a specific portion of DjVu page to raster image formats. Aspose.Imaging for Python via .NET API has exposed an overload version of DjvuMultiPageOptions constructor that can accept an integer parameter for DjVu page index and an instance of Rectangle to specify the desired area to be exported. You can convert a specific portion of DjVu page to image using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of ImageOptionsBase.
  3. Create an instance of DjvuMultiPageOptions while passing the index of DjVu page along with an instance of Rectangle as parameter, and set it as MultiPageOptions property of the ImageOptionsBase created in step 2.
  4. Call Image.Save by passing the file path as well as the instance of ImageOptionsBase.

Here is the source code to convert a portion of 1st page of DjVu to PNG format.

import aspose.pycore as aspycore
from aspose.imaging import Image, IntRange, Rectangle
from aspose.imaging.fileformats.png import PngColorType
from aspose.imaging.fileformats.djvu import DjvuImage
from aspose.imaging.imageoptions import PngOptions, DjvuMultiPageOptions
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
# Load a DjVu image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu")), DjvuImage) as image:
# Create an instance of PngOptions and Set ColorType to Grayscale
export_options = PngOptions()
export_options.color_type = PngColorType.GRAYSCALE
# Create an instance of Rectangle and specify the portion on DjVu page
export_area = Rectangle(0, 0, 500, 500)
# Specify the DjVu page index and Initialize an instance of DjvuMultiPageOptions while passing index of DjVu page index and instance of Rectangle covering the area to be exported
export_page_index = 0
export_options.multi_page_options = DjvuMultiPageOptions(export_page_index, export_area)
image.save(os.path.join(data_dir, "result.png"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.png"))

Converting DjVu to PDF Format

Aspose.Imaging APIs provides the functionality to export the DjVu files to raster images as well as PDF format. This article demonstrates the usage of Aspose.Imaging for .NET API to export the DjVu pages to PDF format. You can convert a specific range of DjVu pages to PDF format using the simple steps as elaborated below.

  1. Load the DjVu file into an instance of DjvuImage.
  2. Create an instance of PdfOptions.
  3. Create an instance of IntRange and initialize it with range of pages to be exported.
  4. Create an instance of DjvuMultiPageOptions while passing the indices of DjVu pages as parameter and set it as MultiPageOptions property of the PdfOptions created in step 2.
  5. Call Image.save by passing the file path as well as the instance of PdfOptions.

Here is the source code to convert first 5 pages of DjVu to PDF format.

import aspose.pycore as aspycore
from aspose.imaging import Image, IntRange
from aspose.imaging.fileformats.djvu import DjvuImage
from aspose.imaging.fileformats.pdf import PdfDocumentInfo
from aspose.imaging.imageoptions import PdfOptions, DjvuMultiPageOptions
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
# Load a DjVu image
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu")), DjvuImage) as image:
# Create an instance of PdfOptions and Initialize the metadata for Pdf document
export_options = PdfOptions()
export_options.pdf_document_info = PdfDocumentInfo()
# Create an instance of IntRange and initialize it with the range of DjVu pages to be exported
range_ = IntRange(0, 1)
# Initialize an instance of DjvuMultiPageOptions with range of DjVu pages to be exported and Save the result in PDF format
export_options.multi_page_options = DjvuMultiPageOptions(range_)
image.save(os.path.join(data_dir, "result.pdf"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.pdf"))

Image Processing using Multithreading

Aspose.Imaging is multithread safe as long as only one thread works on a Document at a time. It is a typical scenario to have one thread working on one document.This article demonstrates how Aspose.Imaging for Python via .NET supports parallel DJVU images processing using multithreading.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.imageoptions import PngOptions
import os
import threading
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
num_threads = 5
def thread_func(path, output_file):
try:
with Image.load(path) as image:
image.save(output_file, PngOptions())
finally:
if delete_output:
os.remove(output_file)
input_file = os.path.join(data_dir, "template.djvu")
threads = []
for task_num in range(num_threads):
output_file = os.path.join(data_dir, f"result_task{task_num}.png")
thread = threading.Thread(target=thread_func, args=(input_file, output_file))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()

Memory Strategy optimization

Loading of Djvu images can be proceeded using memory strategy optimization - i.e. limiting memory buffer size for operation.

import aspose.pycore as aspycore
from aspose.imaging import Image, LoadOptions
from aspose.imaging.fileformats.djvu import DjvuImage
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
# Setting a memory limit of 50 megabytes for target loaded image
obj_init = LoadOptions()
obj_init.buffer_size_hint = 50
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.djvu"), obj_init), DjvuImage) as image:
page_num = 0
for page in image.pages:
out_file = os.path.join(data_dir, f"page{page_num}.png")
page.save(out_file, PngOptions())
if delete_output:
os.remove(out_file)
page_num += 1