Convert image to pdf, which looks like a scanned
Contents
[
Hide
]
Convert image to pdf, which looks like a scanned
Issue : How to emulate scanned document from your image
Tips : Appose.Imaging allows convert your single page or multi page images into pdf, which looks like a scanned document.
Example of conversion of image to pdf, which looks like a scanned
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.pycore as aspycore | |
from aspose.imaging import Image, ResolutionSetting, Color, IntRange | |
from aspose.imaging.fileformats.pdf import PdfDocumentInfo | |
from aspose.imaging.fileformats.djvu import DjvuImage | |
from aspose.imaging.imagefilters.filteroptions import GaussianBlurFilterOptions | |
from aspose.imaging.imageoptions import PdfOptions, DjvuMultiPageOptions | |
import os | |
import random | |
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 | |
random.seed() | |
# Path to input djvu file | |
input_file = os.path.join(templates_folder, "template.djvu") | |
with aspycore.as_of(Image.load(input_file), DjvuImage) as image: | |
document_page_count = image.pages.length | |
# Apply scanner effects | |
# Apply scanner effects | |
# for loop | |
for page in image.djvu_pages: | |
page.rotate(-0.5 + random.randint(0, 100) % 2, True, Color.white) | |
page.filter(page.bounds, GaussianBlurFilterOptions(5, 5)) | |
# Export to Pdf | |
default_page_ppi = 300 | |
export_options = PdfOptions() | |
export_options.resolution_settings = ResolutionSetting(default_page_ppi, default_page_ppi) | |
export_options.pdf_document_info = PdfDocumentInfo() | |
range_pages = IntRange(0, 1) | |
export_options.multi_page_options = DjvuMultiPageOptions(range_pages) | |
image.save(os.path.join(templates_folder, "result.pdf"), export_options) | |
if delete_output: | |
os.remove(os.path.join(templates_folder, "result.pdf")) | |
|
|
---|---|
Input image before application of scan effect | Looking like scanned result |