From 8f798e8c210069ddab37d63623c60e4f3fd39b19 Mon Sep 17 00:00:00 2001 From: xenofem Date: Sun, 4 Aug 2024 01:18:18 -0400 Subject: [PATCH] figure out optimal DPI for pdf page conversion --- dlibrary/dlibrary.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/dlibrary/dlibrary.py b/dlibrary/dlibrary.py index 8bb45aa..049e583 100755 --- a/dlibrary/dlibrary.py +++ b/dlibrary/dlibrary.py @@ -779,6 +779,25 @@ def display_sixel_pixmap(pixmap_bytes): finally: sixel_output_unref(output) +def naive_convert_page(page): + return { 'ext': 'png', 'image': page.get_pixmap(dpi=PDF_CONVERSION_DPI).tobytes('png') } + +def convert_page(pdf, page): + xref = get_displayed_image_xref(page) + if xref is None: + debug('Page has multiple images, converting with naive DPI approach') + return naive_convert_page(page) + + image_rect = page.get_image_rects(xref)[0] + image = extract_image(pdf, xref) + scale_factor = image['width'] / image_rect.width + if scale_factor < PDF_CONVERSION_DPI / 72: + debug(f'Image scale factor is lower than {PDF_CONVERSION_DPI}dpi, using higher dpi instead') + return naive_convert_page(page) + + debug(f'Page has single image, converting with scale factor {scale_factor}') + return { 'ext': 'png', 'image': page.get_pixmap(matrix=fitz.Matrix(scale_factor, scale_factor)).tobytes('png') } + def pdf_image_extractors(pdf, strategy): print(f'0/{pdf.page_count} pages analyzed...', end='') image_extractors = [] @@ -796,7 +815,7 @@ def pdf_image_extractors(pdf, strategy): if choice.lower().startswith('c'): if choice == strategy: print(f'Converting page {idx+1}') - image_extractors.append(lambda p=page: { 'ext': 'png', 'image': p.get_pixmap(dpi=PDF_CONVERSION_DPI).tobytes('png') }) + image_extractors.append(lambda p=pdf, pp=page: convert_page(p, pp)) break if xref is not None and (choice.lower().startswith('x') or choice.lower() == 'extract'): if choice == strategy: