Repository: python-pillow/Pillow

Issue:
Document saving LZW-compressed TIFF files
This [works](http://stackoverflow.com/a/23612127/1389680), but it is missing [in the docs](http://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html#saving-tiff-images).



Discussion:
Actually, that's ... not quite right. 

First, there are two flags, READ_LIBTIFF and WRITE_LIBTIFF, which are (currently) only used for testing. Setting READ_LIBTIFF before a write is a noop. 

Second, it may be a bug that we're not setting tiff_lzw to use libtiff by default. There is LZW and packbits support in old PIL code, but IIRC, it's somewhat broken. (It my be broken by not being called, testing is required ).

Third, the changes in Pillow postdate that question about PIL by a good 3 years, and the libtiff support was only merged last year. 

Sorry, of course I meant WRITE_LIBTIFF. That's what I currently use to activate LZW compression.
When I do not set it, I get an _uncompressed_ file with a compression tag value of COMPRESSION_LZW.
So I think [this line](https://github.com/python-imaging/Pillow/blob/029a4a5079a6d33edad9c7cf073bf9f11fe0d1a9/PIL/TiffImagePlugin.py#L988) should have "tiff_lzw" added.

Ref #431. Should check there to see if packbits is returning the same thing. If so, we should just run any compression through libtiff.

Yes, without WRITE_LIBTIFF the compression = 'packbits' saves an uncompressed file that has the PackBits tag set.

Tested with:

```
from PIL import Image, TiffImagePlugin

img = Image.new('L', (300, 300))
img.putpixel((150,150), 255)

img.save('uncompressed.tiff')
TiffImagePlugin.WRITE_LIBTIFF = True
img.save('correct.tiff', compression = 'packbits')
TiffImagePlugin.WRITE_LIBTIFF = False
img.save('wrongCompressionTag.tiff', compression = 'packbits')
```

Right then, I think that's answered. Time to run the writes through libtiff.   


Resolving patch:
diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py
--- a/PIL/TiffImagePlugin.py
+++ b/PIL/TiffImagePlugin.py
@@ -984,11 +984,7 @@ def _save(im, fp, filename):
 
     compression = im.encoderinfo.get('compression',im.info.get('compression','raw'))
 
-    libtiff = WRITE_LIBTIFF or compression in ["tiff_ccitt", "group3", "group4",
-                                               "tiff_jpeg", "tiff_adobe_deflate",
-                                               "tiff_thunderscan", "tiff_deflate",
-                                               "tiff_sgilog", "tiff_sgilog24",
-                                               "tiff_raw_16"]
+    libtiff = WRITE_LIBTIFF or compression != 'raw' 
 
     # required for color libtiff images
     ifd[PLANAR_CONFIGURATION] = getattr(im, '_planar_configuration', 1)


Test patch:
diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py
--- a/Tests/test_file_libtiff.py
+++ b/Tests/test_file_libtiff.py
@@ -258,9 +258,6 @@ def test_compressions():
     im = lena('RGB')
     out = tempfile('temp.tif')
 
-    TiffImagePlugin.READ_LIBTIFF = True
-    TiffImagePlugin.WRITE_LIBTIFF = True
-
     for compression in ('packbits', 'tiff_lzw'):
         im.save(out, compression=compression)
         im2 = Image.open(out)
@@ -270,11 +267,6 @@ def test_compressions():
     im2 = Image.open(out)
     assert_image_similar(im, im2, 30)
                             
-    TiffImagePlugin.READ_LIBTIFF = False
-    TiffImagePlugin.WRITE_LIBTIFF = False
-
-
-
 
 def test_cmyk_save():
     im = lena('CMYK')