From 30072e0c701c1bc9b874281e11d2cd767e070542 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Sun, 16 Aug 2015 00:54:03 -0700 Subject: [PATCH] Pillow sucks Far from being fluffy or friendly, Pillow silently allows installation of itself without support for major image types. Reportlab calls for pillow 2.4.0. On Ubuntu 14.04 LTS this will trigger an upgrade of pillow that will be built without JPEG or ZLIB so it is effectively neutered, and unfortunately Pillow will not detect this situation at install time and guide users to a resolution. Instead, you see nasty stack traces. So add a run-time check to ensure that Pillow is sane and capable of JPEG and PNG support since both may be used internally. --- ocrmypdf/main.py | 35 +++++++++++++++++++++++++++++++++++ setup.py | 4 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ocrmypdf/main.py b/ocrmypdf/main.py index 490ced21..df565d0f 100755 --- a/ocrmypdf/main.py +++ b/ocrmypdf/main.py @@ -63,6 +63,41 @@ if tesseract.version() < MINIMUM_TESS_VERSION: sys.exit(ExitCode.missing_dependency) +try: + import PIL.features + check_codec = PIL.features.check_codec +except (ImportError, AttributeError): + def check_codec(codec_name): + if codec_name == 'jpg': + return 'jpeg_encoder' in dir(Image.core) + elif codec_name == 'zlib': + return 'zip_encoder' in dir(Image.core) + raise NotImplementedError(codec_name) + + +def check_pil_encoder(codec_name, friendly_name): + try: + if check_codec(codec_name): + return + except Exception: + pass + complain( + "ERROR: Your version of the Python imaging library (Pillow) was " + "compiled without support for " + friendly_name + " encoding/decoding." + "\n" + "You will need to uninstall Pillow and reinstall it with PNG and JPEG " + "support (libjpeg and zlib)." + "\n" + "See installation instructions for your platform here:\n" + " https://pillow.readthedocs.org/installation.html" + ) + sys.exit(ExitCode.missing_dependency) + + +check_pil_encoder('jpg', 'JPEG') +check_pil_encoder('zlib', 'PNG') + + # ------------- # Parser diff --git a/setup.py b/setup.py index 2b9ea53e..17625246 100644 --- a/setup.py +++ b/setup.py @@ -202,12 +202,12 @@ setup( ], install_requires=[ 'ruffus>=2.6.3', - 'Pillow>=2.3', + 'Pillow>=2.4.0', 'lxml>=3.3.3', 'reportlab>=3.1.44', 'PyPDF2>=1.25.1' ], - test_requires=[ + tests_require=[ 'img2pdf>=0.1.5', 'pytest>=2.7.2' ],