Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for saving TIFF image into an io.BytesIO buffer #1011

Merged
merged 2 commits into from Nov 16, 2014

Conversation

mfergie
Copy link

@mfergie mfergie commented Nov 14, 2014

Hi,

When trying to save to a io.BytesIO() I am getting the following error (on python 2.7.8 and 3.4.2):

Traceback (most recent call last):
  File "pillow-tiff-string-buffer.py", line 15, in <module>
    pilim.save(string_buffer, format="tiff", compression='tiff_lzw')
  File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 1685, in save
    save_handler(self, fp, filename)
  File "/usr/local/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 1127, in _save
    _fp = os.dup(fp.fileno())
io.UnsupportedOperation: fileno

The enclosed changes rectify this issue by catching the exception instead of relying on the hasattr test.

I'm not sure where I should create the unit-test, but I have created a minimal gist to reproduce:
https://gist.github.com/mfergie/c3baa78d1c1b1a9a239b

Note, this only happens when specifying compression when saving.

@hugovk
Copy link
Member

hugovk commented Nov 14, 2014

@mfergie Please can you include tests for this? If there's not already, it'd be good to test both without compression and with different kinds of compression. Check these:

tests/test_file_tiff.py
tests/test_file_libtiff.py

Thanks.

@mfergie
Copy link
Author

mfergie commented Nov 14, 2014

@hugovk Will look into it this afternoon.

@mfergie
Copy link
Author

mfergie commented Nov 14, 2014

@hugovk I've added a test here:
https://github.com/mfergie/Pillow/blob/master/Tests/test_file_tiff.py#L329

Could you please check it's suitability? Thanks

@wiredfool
Copy link
Member

I want to review this as well.

@wiredfool
Copy link
Member

I've made a few minor changes -- hoisted the imports and moved the tests to the test_file_libtiff file, since libtiff is an optional build and that file checks for libtiff in the build.

I also changed the test file to our standard image -- was the F mode image an important part of the test? We should probably do an exhaustive set of tests on all the various formats, but not necessary for this PR.

@wiredfool wiredfool merged commit 8b0ecb6 into python-pillow:master Nov 16, 2014
@wiredfool
Copy link
Member

Thanks.

@mfergie
Copy link
Author

mfergie commented Nov 16, 2014

F mode was not important for the tests. Thanks for your assistance, Martin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants