Skip to content

Commit

Permalink
Fix DPI mismatch between doc2pixels and pixels2pdf
Browse files Browse the repository at this point in the history
The original document was larger in dimensions than the original one due
to a mismatch in DPI settings. When converting documents to pixels we
were setting the DPI to 150 pixels per inch. Then when converting back
into a PDF we were using 70 DPI. This difference would result in an
overall larger document in dimensions (though not necessarily in file
size).

Fixes #626
  • Loading branch information
deeplow committed Dec 4, 2023
1 parent 0d1de3d commit 9bb5195
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions dangerzone/conversion/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TIMEOUT_PER_PAGE: float = 30 # (seconds)
TIMEOUT_PER_MB: float = 30 # (seconds)
TIMEOUT_MIN: float = 60 # (seconds)
DEFAULT_DPI = 150 # Pixels per inch


def running_on_qubes() -> bool:
Expand Down
4 changes: 2 additions & 2 deletions dangerzone/conversion/doc_to_pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import magic

from . import errors
from .common import DangerzoneConverter, running_on_qubes
from .common import DEFAULT_DPI, DangerzoneConverter, running_on_qubes


class DocumentToPixels(DangerzoneConverter):
Expand Down Expand Up @@ -253,7 +253,7 @@ async def convert(self) -> None:
self.update_progress(
f"Converting page {page_num}/{doc.page_count} to pixels"
)
pix = page.get_pixmap(dpi=150)
pix = page.get_pixmap(dpi=DEFAULT_DPI)
rgb_buf = pix.samples
await self.write_page_width(pix.width, width_filename)
await self.write_page_height(pix.height, height_filename)
Expand Down
3 changes: 2 additions & 1 deletion dangerzone/conversion/pixels_to_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import fitz

from .common import DangerzoneConverter, get_tessdata_dir, running_on_qubes
from .common import DEFAULT_DPI, DangerzoneConverter, get_tessdata_dir, running_on_qubes


class PixelsToPDF(DangerzoneConverter):
Expand Down Expand Up @@ -56,6 +56,7 @@ async def convert(
pixmap = fitz.Pixmap(
fitz.Colorspace(fitz.CS_RGB), width, height, untrusted_rgb_data, False
)
pixmap.set_dpi(DEFAULT_DPI, DEFAULT_DPI)
if ocr_lang: # OCR the document
try:
ocr_pdf_bytes = pixmap.pdfocr_tobytes(
Expand Down

0 comments on commit 9bb5195

Please sign in to comment.