From dd3a46520c6a5c5e43de7001c369f0a1803e8e57 Mon Sep 17 00:00:00 2001 From: Martin Fergie Date: Fri, 14 Nov 2014 12:29:10 +0000 Subject: [PATCH 1/3] Fix for saving to io.BytesIO() --- PIL/TiffImagePlugin.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 6a52e5e5c..21ff13ec6 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -1128,8 +1128,12 @@ def _save(im, fp, filename): print (ifd.items()) _fp = 0 if hasattr(fp, "fileno"): - fp.seek(0) - _fp = os.dup(fp.fileno()) + import io + try: + fp.seek(0) + _fp = os.dup(fp.fileno()) + except io.UnsupportedOperation: + pass # ICC Profile crashes. blocklist = [STRIPOFFSETS, STRIPBYTECOUNTS, ROWSPERSTRIP, ICCPROFILE] From 8b0ecb6192b13066f288cc51ed3c453b70bdef50 Mon Sep 17 00:00:00 2001 From: Martin Fergie Date: Fri, 14 Nov 2014 14:37:03 +0000 Subject: [PATCH 2/3] Added test for different compression types. --- Tests/test_file_tiff.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index 3939ac074..f3958f548 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -326,7 +326,34 @@ class TestFileTiff(PillowTestCase): # Should not divide by zero im.save(outfile) + def test_save_bytesio(self): + # PR 1011 + # Test TIFF saving to io.BytesIO() object. + + # Generate test image + pilim = Image.new('F', (100, 100), 0) + def save_bytesio(compression=None): + import io + + testfile = self.tempfile("temp_.tiff".format(compression)) + + buffer_io = io.BytesIO() + pilim.save(buffer_io, format="tiff", compression=compression) + buffer_io.seek(0) + data = buffer_io.read() + buffer_io.close() + with open(testfile, "wb") as fd: + fd.write(data) + + pilim_load = Image.open(testfile) + self.assert_image_similar(pilim, pilim_load, 0) + + # save_bytesio() + save_bytesio("packbits") + save_bytesio("tiff_lzw") + + if __name__ == '__main__': unittest.main() From 9a2cc7df83167f56deb5a32e02c1c487ea152260 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sat, 15 Nov 2014 20:38:52 -0800 Subject: [PATCH 3/3] hoist import, move test to libtiff --- PIL/TiffImagePlugin.py | 2 +- Tests/test_file_libtiff.py | 32 ++++++++++++++++++++++++++++++-- Tests/test_file_tiff.py | 27 --------------------------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 21ff13ec6..ae7d56947 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -54,6 +54,7 @@ import sys import collections import itertools import os +import io # Set these to true to force use of libtiff for reading or writing. READ_LIBTIFF = False @@ -1128,7 +1129,6 @@ def _save(im, fp, filename): print (ifd.items()) _fp = 0 if hasattr(fp, "fileno"): - import io try: fp.seek(0) _fp = os.dup(fp.fileno()) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index efd2d5817..12f25165f 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1,6 +1,7 @@ from helper import unittest, PillowTestCase, hopper, py3 import os +import io from PIL import Image, TiffImagePlugin @@ -59,9 +60,8 @@ class TestFileLibTiff(LibTiffTestCase): def test_g4_tiff_bytesio(self): """Testing the stringio loading code path""" - from io import BytesIO file = "Tests/images/hopper_g4_500.tif" - s = BytesIO() + s = io.BytesIO() with open(file, 'rb') as f: s.write(f.read()) s.seek(0) @@ -357,6 +357,34 @@ class TestFileLibTiff(LibTiffTestCase): self.assertEqual(im.mode, "L") self.assert_image_similar(im, original, 7.3) + def test_save_bytesio(self): + # PR 1011 + # Test TIFF saving to io.BytesIO() object. + + TiffImagePlugin.WRITE_LIBTIFF = True + TiffImagePlugin.READ_LIBTIFF = True + + # Generate test image + pilim = hopper() + + def save_bytesio(compression=None): + + testfile = self.tempfile("temp_.tiff".format(compression)) + + buffer_io = io.BytesIO() + pilim.save(buffer_io, format="tiff", compression=compression) + buffer_io.seek(0) + + pilim_load = Image.open(buffer_io) + self.assert_image_similar(pilim, pilim_load, 0) + + # save_bytesio() + save_bytesio('raw') + save_bytesio("packbits") + save_bytesio("tiff_lzw") + + TiffImagePlugin.WRITE_LIBTIFF = False + TiffImagePlugin.READ_LIBTIFF = False diff --git a/Tests/test_file_tiff.py b/Tests/test_file_tiff.py index f3958f548..fd8090196 100644 --- a/Tests/test_file_tiff.py +++ b/Tests/test_file_tiff.py @@ -326,33 +326,6 @@ class TestFileTiff(PillowTestCase): # Should not divide by zero im.save(outfile) - def test_save_bytesio(self): - # PR 1011 - # Test TIFF saving to io.BytesIO() object. - - # Generate test image - pilim = Image.new('F', (100, 100), 0) - def save_bytesio(compression=None): - import io - - testfile = self.tempfile("temp_.tiff".format(compression)) - - buffer_io = io.BytesIO() - pilim.save(buffer_io, format="tiff", compression=compression) - buffer_io.seek(0) - data = buffer_io.read() - buffer_io.close() - - with open(testfile, "wb") as fd: - fd.write(data) - - pilim_load = Image.open(testfile) - self.assert_image_similar(pilim, pilim_load, 0) - - # save_bytesio() - save_bytesio("packbits") - save_bytesio("tiff_lzw") - if __name__ == '__main__': unittest.main()