A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Adds specific handling for CCITT Group 4, which pikepdf decodes, but not correctly
Trenton Holmes4cc2976614b3
diff --git a/src/documents/barcodes.py b/src/documents/barcodes.pyindex 54db83c19..a4be126a5 100644--- a/src/documents/barcodes.py+++ b/src/documents/barcodes.py@@ -20,6 +20,10 @@ from pyzbar import pyzbarlogger = logging.getLogger("paperless.barcodes")+class BarcodeImageFormatError(Exception):+ pass++@lru_cache(maxsize=8)def supported_file_type(mime_type) -> bool:"""@@ -115,6 +119,9 @@ def scan_file_for_separating_barcodes(filepath: str) -> Tuple[Optional[str], Lisfor image_key in page.images:pdfimage = PdfImage(page.images[image_key])+ if "/CCITTFaxDecode" in pdfimage.filters:+ raise BarcodeImageFormatError()+# Not all images can be transcoded to a PIL image, which# is what pyzbar expects to receivepillow_img = pdfimage.as_pil_image()diff --git a/src/documents/tests/samples/barcodes/barcode-fax-image.pdf b/src/documents/tests/samples/barcodes/barcode-fax-image.pdfnew file mode 100644index 000000000..2e248c82bBinary files /dev/null and b/src/documents/tests/samples/barcodes/barcode-fax-image.pdf differdiff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.pyindex 0f16845d2..ee8df9f34 100644--- a/src/documents/tests/test_barcodes.py+++ b/src/documents/tests/test_barcodes.py@@ -226,7 +226,7 @@ class TestBarcode(DirectoriesMixin, TestCase):WHEN:- The image tries to be transcoded to a PIL image, but failsTHEN:- - The barcode reader is still called, as+ - The barcode reader is still called"""def _build_device_n_pdf(self, save_path: str):@@ -279,6 +279,26 @@ class TestBarcode(DirectoriesMixin, TestCase):reader.assert_called()+ def test_scan_file_for_separating_barcodes_fax_decode(self):+ """+ GIVEN:+ - A PDF containing an image encoded as CCITT Group 4 encoding+ WHEN:+ - Barcode processing happens with the file+ THEN:+ - The barcode is still detected+ """+ test_file = os.path.join(+ self.BARCODE_SAMPLE_DIR,+ "barcode-fax-image.pdf",+ )+ pdf_file, separator_page_numbers = barcodes.scan_file_for_separating_barcodes(+ test_file,+ )++ self.assertEqual(pdf_file, test_file)+ self.assertListEqual(separator_page_numbers, [1])+def test_scan_file_for_separating_qr_barcodes(self):test_file = os.path.join(self.BARCODE_SAMPLE_DIR,