From 073f1b90b1046383ecc507dd26d0824c361417a4 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Mon, 24 Apr 2017 13:58:00 -0400 Subject: [PATCH 1/9] Add LZW compression option to save TIFF images 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 --- lib/matplotlib/backends/backend_agg.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 0d7cfeac5965..b1856d516fa1 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -625,12 +625,29 @@ def print_jpg(self, filename_or_obj, *args, **kwargs): # 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) - return image.save(filename_or_obj, format='tiff', + + 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) print_tiff = print_tif From 14d0c30c417ee2131d430cd3bc914b273c0fc8d3 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Mon, 24 Apr 2017 13:59:49 -0400 Subject: [PATCH 2/9] Improved adhesion to code standards minor fix in the char spacing --- lib/matplotlib/backends/backend_agg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index b1856d516fa1..7e677044c107 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -640,11 +640,11 @@ def print_tif(self, filename_or_obj, *args, **kwargs): compressed = kwargs.pop("compressed", False): if compressed: - original_value = TiffImagePlugin.WRITE_LIBTIFF + libtiff_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 + dpi=dpi, compression='tiff_lzw') + TiffImagePlugin.WRITE_LIBTIFF = libtiff_original_value return return_value else: return image.save(filename_or_obj, format='tiff', From ad850c2de97e44a665504a1afbd37933cbdab273 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Mon, 24 Apr 2017 14:04:29 -0400 Subject: [PATCH 3/9] Added TiffImagePlugin import required in print_tif --- lib/matplotlib/backends/backend_agg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 7e677044c107..6f60a9331601 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -45,7 +45,7 @@ from matplotlib import _png try: - from PIL import Image + from PIL import Image, TiffImagePlugin _has_pil = True except ImportError: _has_pil = False From f4055ece2de1a222c02ca89279cc483c57e61326 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Mon, 24 Apr 2017 14:22:40 -0400 Subject: [PATCH 4/9] Fix typo that broke building process --- lib/matplotlib/backends/backend_agg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 6f60a9331601..1d84e81d137c 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -638,7 +638,7 @@ def print_tif(self, filename_or_obj, *args, **kwargs): image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) dpi = (self.figure.dpi, self.figure.dpi) - compressed = kwargs.pop("compressed", False): + compressed = kwargs.pop("compressed", False) if compressed: libtiff_original_value = TiffImagePlugin.WRITE_LIBTIFF TiffImagePlugin.WRITE_LIBTIFF = True From 1ed0ef97317643fc49eb152e38ceff2b8afa69b1 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Mon, 24 Apr 2017 14:28:55 -0400 Subject: [PATCH 5/9] Add comments and better spacing for TIFF LZW --- lib/matplotlib/backends/backend_agg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 1d84e81d137c..953973a0b188 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -637,18 +637,18 @@ def print_tif(self, filename_or_obj, *args, **kwargs): return image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) dpi = (self.figure.dpi, self.figure.dpi) - + #add TIFF compression support compressed = kwargs.pop("compressed", False) if compressed: libtiff_original_value = TiffImagePlugin.WRITE_LIBTIFF TiffImagePlugin.WRITE_LIBTIFF = True return_value = image.save(filename_or_obj, format='tiff', - dpi=dpi, compression='tiff_lzw') + dpi=dpi, compression='tiff_lzw') TiffImagePlugin.WRITE_LIBTIFF = libtiff_original_value return return_value else: return image.save(filename_or_obj, format='tiff', - dpi=dpi) + dpi=dpi) print_tiff = print_tif From fe523c42a2f12fd1f8f7f88eca3853f7f346a2a4 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Tue, 25 Apr 2017 21:26:18 -0400 Subject: [PATCH 6/9] Added "compression" as parameter that is passed to PIL backend --- lib/matplotlib/backends/backend_agg.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 953973a0b188..16f2b8d8f974 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -628,28 +628,25 @@ 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. + compression : 'tiff_deflate', 'tiff_adobe_deflate', 'tiff_lzw', + and other values allowed by LIBTIFF_CORE. + If present, indicates the compression algorithm that + will be passed to the backend to save the image. """ 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) - #add TIFF compression support - compressed = kwargs.pop("compressed", False) - if compressed: - libtiff_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 = libtiff_original_value - return return_value + #add TIFF compression support by passing the parameter to backend + compressed = kwargs.pop("compression", None) + if compression is not None: + return image.save(filename_or_obj, format='tiff', + dpi=dpi, compression=compression) else: return image.save(filename_or_obj, format='tiff', - dpi=dpi) + dpi=dpi) + print_tiff = print_tif - FigureCanvas = FigureCanvasAgg From f3e730f8b3209b0b5a7af623c7b4304d5f654d2f Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Wed, 26 Apr 2017 09:46:15 -0400 Subject: [PATCH 7/9] Fixed variable names and polished code a bit more --- lib/matplotlib/backends/backend_agg.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index 16f2b8d8f974..d2d5808dece6 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -639,13 +639,8 @@ def print_tif(self, filename_or_obj, *args, **kwargs): image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) dpi = (self.figure.dpi, self.figure.dpi) #add TIFF compression support by passing the parameter to backend - compressed = kwargs.pop("compression", None) - if compression is not None: - return image.save(filename_or_obj, format='tiff', - dpi=dpi, compression=compression) - else: - return image.save(filename_or_obj, format='tiff', - dpi=dpi) + compression = kwargs.pop("compression", None) + return image.save(filename_or_obj, format='tiff', dpi=dpi, compression=compression) print_tiff = print_tif From ca69a47bb11b3922488b372d8cbcfa08e3f8a266 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Wed, 26 Apr 2017 09:53:38 -0400 Subject: [PATCH 8/9] Removed unused import --- lib/matplotlib/backends/backend_agg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index d2d5808dece6..b62c5fa125f0 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -45,7 +45,7 @@ from matplotlib import _png try: - from PIL import Image, TiffImagePlugin + from PIL import Image _has_pil = True except ImportError: _has_pil = False From bdb806639d57a41e339c782dad144bc43ab60918 Mon Sep 17 00:00:00 2001 From: Dana-Farber Date: Wed, 26 Apr 2017 09:55:15 -0400 Subject: [PATCH 9/9] Code polishing --- lib/matplotlib/backends/backend_agg.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index b62c5fa125f0..170a67974542 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -640,7 +640,8 @@ def print_tif(self, filename_or_obj, *args, **kwargs): dpi = (self.figure.dpi, self.figure.dpi) #add TIFF compression support by passing the parameter to backend compression = kwargs.pop("compression", None) - return image.save(filename_or_obj, format='tiff', dpi=dpi, compression=compression) + return image.save(filename_or_obj, format='tiff', dpi=dpi, + compression=compression) print_tiff = print_tif