mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
Merge pull request #1012 from wiredfool/libtiff-save-bytesio
Fix for saving TIFF image into an io.BytesIO buffer
This commit is contained in:
commit
a32f797801
|
@ -54,6 +54,7 @@ import sys
|
||||||
import collections
|
import collections
|
||||||
import itertools
|
import itertools
|
||||||
import os
|
import os
|
||||||
|
import io
|
||||||
|
|
||||||
# Set these to true to force use of libtiff for reading or writing.
|
# Set these to true to force use of libtiff for reading or writing.
|
||||||
READ_LIBTIFF = False
|
READ_LIBTIFF = False
|
||||||
|
@ -1128,8 +1129,11 @@ def _save(im, fp, filename):
|
||||||
print (ifd.items())
|
print (ifd.items())
|
||||||
_fp = 0
|
_fp = 0
|
||||||
if hasattr(fp, "fileno"):
|
if hasattr(fp, "fileno"):
|
||||||
fp.seek(0)
|
try:
|
||||||
_fp = os.dup(fp.fileno())
|
fp.seek(0)
|
||||||
|
_fp = os.dup(fp.fileno())
|
||||||
|
except io.UnsupportedOperation:
|
||||||
|
pass
|
||||||
|
|
||||||
# ICC Profile crashes.
|
# ICC Profile crashes.
|
||||||
blocklist = [STRIPOFFSETS, STRIPBYTECOUNTS, ROWSPERSTRIP, ICCPROFILE]
|
blocklist = [STRIPOFFSETS, STRIPBYTECOUNTS, ROWSPERSTRIP, ICCPROFILE]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from helper import unittest, PillowTestCase, hopper, py3
|
from helper import unittest, PillowTestCase, hopper, py3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import io
|
||||||
|
|
||||||
from PIL import Image, TiffImagePlugin
|
from PIL import Image, TiffImagePlugin
|
||||||
|
|
||||||
|
@ -59,9 +60,8 @@ class TestFileLibTiff(LibTiffTestCase):
|
||||||
|
|
||||||
def test_g4_tiff_bytesio(self):
|
def test_g4_tiff_bytesio(self):
|
||||||
"""Testing the stringio loading code path"""
|
"""Testing the stringio loading code path"""
|
||||||
from io import BytesIO
|
|
||||||
file = "Tests/images/hopper_g4_500.tif"
|
file = "Tests/images/hopper_g4_500.tif"
|
||||||
s = BytesIO()
|
s = io.BytesIO()
|
||||||
with open(file, 'rb') as f:
|
with open(file, 'rb') as f:
|
||||||
s.write(f.read())
|
s.write(f.read())
|
||||||
s.seek(0)
|
s.seek(0)
|
||||||
|
@ -357,6 +357,34 @@ class TestFileLibTiff(LibTiffTestCase):
|
||||||
self.assertEqual(im.mode, "L")
|
self.assertEqual(im.mode, "L")
|
||||||
self.assert_image_similar(im, original, 7.3)
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user