Skip to content

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

Closed

Description

@renato-umeton

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!!!

Activity

  1. jenshnielsen commented on Apr 24, 2017

    @jenshnielsen
    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.

  2. added this to the milestone on Apr 24, 2017
  3. renato-umeton commented on Apr 24, 2017

    @renato-umeton
    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)
    
  4. modified the milestones: needs sorting, v3.1 on Jan 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions