-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Added compression option to save TIFF images #8531
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
Conversation
TIFF is the scientific journals' preferred format; LZW compression is needed to avoid generating 500MB+ images which cannot be readily uploaded to the journal websites during the submission process
minor fix in the char spacing
I think this needs a test even if .tiff doesn't have one 'cause there needs to be a way to reliably verify what's happening, 'specially since apparently compression in LZW is apparently wonky and needs the WRITE_LIBTIFF flag set first. Can you use test_png.py as a model? |
#add TIFF compression support | ||
compressed = kwargs.pop("compressed", False) | ||
if compressed: | ||
libtiff_original_value = TiffImagePlugin.WRITE_LIBTIFF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok to simplify this as?
TiffImagePlugin.WRITE_LIBTIFF = compressed
Or are there instances where the user needs TiffImagePlugin.WRITE_LIBTIFF to be True even when writing out a non-compressed image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll add the test part.
I'd say it is not OK to "simplify" because you may use the flag somewhere else and remain confused about it not getting back to its original state once finished saving the image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably better to use try:... finally:... to handle the case where the saving fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@story645 I'm having some issues coming up with some meaningful tests since we now do a pull pass-through of the parameters to the backend. Can you suggest some tests, if needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check that the arg is getting passed through properly and that what this function saves out is a valid compressed tiff file (just try one compression arg parameter, does not matter which one).
May I suggest to replace the |
👍 to @cgohlke 's suggestion. It's cleaner, more general, and gives us functionality for free. |
Done, I've incorporated also cgohlke's idea -- that gives us indeed free stuff. Good thinking! |
TiffImagePlugin.WRITE_LIBTIFF = libtiff_original_value | ||
return return_value | ||
#add TIFF compression support by passing the parameter to backend | ||
compressed = kwargs.pop("compression", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compressed = kwargs.pop("compression", None)
should be
compression = kwargs.pop("compression", None)
return return_value | ||
#add TIFF compression support by passing the parameter to backend | ||
compressed = kwargs.pop("compression", None) | ||
if compression is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need an if/else anymore, since compression=None is a valid arg to pass through, so you can just do:
return image.save(filename_or_obj, format='tiff', dpi=dpi, compression=compression)
@story645 Done all the requested changes. Regarding the tests, the part that is not working is writing to io.BytesIO() -- for some reason it works for PDFs but not for TIFFs. This would explain the current lack of tests for the TIFF section... Any ideas? Thanks! |
Can you post your test (here or as a gist?) having trouble understanding what you mean. |
This works:
This does not -- if I save to "/tmp/a.tiff" instead of io.BytesIO then everything goes smooth. Ideas?
|
What length are you getting for |
I get an exception even before being able to compute length: if the filename is absent (ie, we write to io.BytesIO() then the exception is triggered. I'm not sure if I can write a test that writes to /tmp since it would pass on *nix but fail on windows. Is there a temporary directory I can write to at test-time? |
Can you post the error?and your environment/mpl version? Only because I'm not getting any error for:
|
Hi, just wanna follow up on this...still interested in working on it? |
Superseded by #13094, thanks for the PR. |
PR Summary
TIFF is the scientific journals' preferred format; LZW compression is needed to avoid generating huge (500MB+) images that cannot be readily uploaded to the journal websites during the submission process. Currently we generate huge TIFF image files with MPL, then import them in PIL, save them in PIL using compression of the TIFF and then discarding the uncompressed TIFF files. This PR is about adding support for LZW compression when saving TIFF images.
#8530
PR Checklist