Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature request: TIFF LZW compression support in savefig() #8530

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

Closed
renato-umeton opened this issue Apr 24, 2017 · 2 comments · Fixed by #13094
Closed

Feature request: TIFF LZW compression support in savefig() #8530

renato-umeton opened this issue Apr 24, 2017 · 2 comments · Fixed by #13094
Milestone

Comments

@renato-umeton
Copy link

MPL is such a great tool I tend to use it in many projects. Although amazing in so many ways, one area of improvement is the support for compression for TIFF images. Compression works when saving to other formats but it does not work with TIFF format. Note that TIFF is the format most scientific journal prefer for figures. MPL is great at so many things, I think embedding an LZW compression algorithm would be great. Currently we have to resort to PIL or PILLOW and do this:

  1. create the image with MPL
  2. save to tiff, uncompressed -- easily 500MB
  3. load PIL library
  4. load the 500MB image with PIL
  5. re-save the image in PIL using the flag compression=LZW.

If we could spare steps 3-5 it would be great I think...

Thanks!!!

@jenshnielsen
Copy link
Member

Matplotilib is actually using PIL internally to save into TIFF. Look at the end of https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_agg.py#L627
It should be relatively simple to add an optional compression flag to the print_tif method there. Compare with the additional jpeg options just above which also uses PIL and have more options.

If you feel like making the changes your self a pull request is very welcome.

@tacaswell tacaswell added this to the 2.2 (next next feature release) milestone Apr 24, 2017
@renato-umeton
Copy link
Author

Thanks, Jens. My implementation would look like this -- may I use the TiffImagePlugin as stated?

Thanks!

   # add TIFF support
    def print_tif(self, filename_or_obj, *args, **kwargs):
        """
        Other Parameters
        ----------------
        compressed : bool
            If present, indicates that this image
            should be stored using the LZW compression algorithm.
        """
        buf, size = self.print_to_buffer()
        if kwargs.pop("dryrun", False):
            return
        image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
        dpi = (self.figure.dpi, self.figure.dpi)
        
        compressed = kwargs.pop("compressed", False):
        if compressed:
            original_value = TiffImagePlugin.WRITE_LIBTIFF
            TiffImagePlugin.WRITE_LIBTIFF = True    
            return_value = image.save(filename_or_obj, format='tiff',
                          dpi=dpi, compression = "tiff_lzw")
            TiffImagePlugin.WRITE_LIBTIFF = original_value
            return return_value
        else:
            return image.save(filename_or_obj, format='tiff',
                          dpi=dpi)

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 a pull request may close this issue.

6 participants