Skip to content

Commit

Permalink
libtiff5
Browse files Browse the repository at this point in the history
Multipage-tiff fix on resize
  • Loading branch information
AddisonSchiller committed Nov 20, 2017
1 parent a52a515 commit d254a1d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN usermod -d /home www-data \
libfreetype6-dev \
libjpeg-dev \
libpng12-dev \
libtiff5-dev \
libxml2-dev \
libxslt1-dev \
zlib1g-dev \
Expand Down
36 changes: 25 additions & 11 deletions mfr/extensions/pdf/export.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import imghdr
from http import HTTPStatus

from PIL import Image
from PIL import (Image,
TiffImagePlugin)
from reportlab.pdfgen import canvas

from mfr.core import extension
Expand All @@ -18,7 +18,6 @@ def __init__(self, *args, **kwargs):
self.metrics.add('pil_version', Image.VERSION)

def tiff_to_pdf(self, tiff_img, max_size):
width, height = tiff_img.size
c = canvas.Canvas(self.output_file_path)
c.setPageSize((max_size[0], max_size[1]))

Expand All @@ -31,8 +30,28 @@ def tiff_to_pdf(self, tiff_img, max_size):
except EOFError:
break

# set the width on each iteration in case its different per page
width, height = tiff_img.size

# Use temp_image so we can resize without worry
temp_image = tiff_img
# Center the image and draw it in the canvas
c.drawInlineImage(tiff_img, (max_size[0] - width) // 2,
if max_size:
# Resize the image to the w/h maximum specified
ratio = min(max_size[0] / temp_image.size[0], max_size[1] / temp_image.size[1])
self.metrics.add('ratio', ratio)
if ratio < 1:
temp_image = tiff_img.copy()

# Resampling can cause corruption in the image, causing pdf.js to not be able to read it
# Image.ANITALIAS and IMAGE.LANCZOS both caused the error
# Image.BICUBIC was chosen as the highest quality one that did not produce the error
temp_image = temp_image.resize((round(temp_image.size[0] * ratio),
round(temp_image.size[1] * ratio)), Image.BICUBIC)

width, height = temp_image.size

c.drawInlineImage(temp_image, (max_size[0] - width) // 2,
(max_size[1] - height) // 2, anchor='c')
c.showPage()
page += 1
Expand All @@ -50,14 +69,9 @@ def export(self):
'max_size_h': max_size[1],
})
try:
TiffImagePlugin.READ_LIBTIFF = True
image = Image.open(self.source_file_path)
if max_size:
# Resize the image to the w/h maximum specified
ratio = min(max_size[0] / image.size[0], max_size[1] / image.size[1])
self.metrics.add('ratio', ratio)
if ratio < 1:
image = image.resize((round(image.size[0] * ratio),
round(image.size[1] * ratio)), Image.ANTIALIAS)

self.tiff_to_pdf(image, max_size)
image.close()

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Pygments==2.0.2
pydocx==0.7.0

# Image
Pillow==2.8.2
olefile==0.44
Pillow==4.3.0
psd-tools==1.4

# IPython
Expand Down
13 changes: 0 additions & 13 deletions tests/extensions/pdf/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ def test_single_page_tiff(self, directory, file_name):
# the first 4 bytes contain the signature
assert binascii.hexlify(file.read(4)) == PDF_SIG

def test_broken_tiff(self, directory):
# Once Pillow is updated to 4.3, this test will fail and can be removed
source_file_path = os.path.join(BASE, 'files', 'test_broken.tif')
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = PdfExporter(source_file_path=source_file_path, ext='.tif',
output_file_path=output_file_path, format=format)

assert not os.path.exists(output_file_path)

with pytest.raises(exceptions.PillowImageError):
exporter.export()

def test_bad_tiff(self, directory):
source_file_path = os.path.join(BASE, 'files', 'invalid.tif')
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
Expand Down

0 comments on commit d254a1d

Please sign in to comment.