Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created March 9, 2023 09:56
Show Gist options
  • Save aspose-com-gists/ac10a0bbf2180951864a381753f43f77 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ac10a0bbf2180951864a381753f43f77 to your computer and use it in GitHub Desktop.

Aspose.Imaging for Python via .NET API allows to easy manipulate your djvu images or photos in your Python application or Web service.

You can:

  • Convert djvu image to raster formats;
  • Convert djvu image to vector formats;
  • Perform crop, rotate, resize and other typical operations to djvu images;
  • Process multi-page djvu images and convert to multi-page formats (i.e. pdf, tiff, gif, apng etc)

Interested ?

You may go further at : https://products.aspose.com/imaging/python-net/

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"))
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"))
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"))
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"))
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
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment