pbreach
pbreach

Reputation: 16987

OSError when using PIL to save image as TIFF with compression

I am trying to change the dpi of my PNG images and convert them to TIFF using Pillow/PIL like so,

from PIL import Image
import os

for fl in os.listdir(os.getcwd()):
    name, ext = fl.split(".")
    im = Image.open(fl)
    im.save(name + ".tiff", dpi=(500,500), compression="tiff_jpeg")

    print("Done '{}'".format(name))

which works fine if the compression kwarg is not set, but I end up with massive 100MB TIFF files from my 1MB PNGs. If I set the compression type to any of the available options, I end up with the following error:

Traceback (most recent call last):

  File "<ipython-input-1-3631f05e05f4>", line 7, in <module>
    im.save(name + ".tiff", dpi=(500,500), compression="tiff_jpeg")

  File "C:\Users\Patrick\Anaconda3\lib\site-packages\PIL\Image.py", line 1687, in save
    save_handler(self, fp, filename)

  File "C:\Users\Patrick\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py", line 1457, in _save
    raise IOError("encoder error %d when writing image file" % s)

OSError: encoder error -2 when writing image file

In the docs for the Image.save method it mentions that compression is only available if the libtiff library is installed which I do have.

Here is the versions for Python and Pillow that I'm working with:

Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32

libtiff: 4.0.6-vc14_2 [vc14]
pillow: 3.2.0-py35_1 

What might be the cause of this error and what steps can I take to resolve? This is the first time I've used Pillow/PIL and am unsure of where to begin.

Upvotes: 4

Views: 7682

Answers (3)

perunperun
perunperun

Reputation: 31

Not a direct answer to this question, but might help someone who is experiencing similar issues in the future.

I've also been getting OSError: encoder error -2 when writing image file when trying to save a TiffImageFile with compression. This error was happening when trying to to apply tiff_ccitt, group3, or group4 compression options.

I fixed it by simply converting the TIFF to monochrome with image.convert('1') before compressing it. Note that the compression algorithms I mentioned are only used for monochrome images, so we are not losing any colour by adding this step.

How I compressed and saved a multi-page TIFF:

# convert all images to monochrome. Without this step CCITT compression will fail.
images = [image.convert('1') for image in images]
images[0].save(out_path,
               compression="tiff_ccitt",
               save_all=True,
               append_images=images[1:])

Upvotes: 1

mbauer
mbauer

Reputation: 116

I had a very similar issue with 'group4' compression on B&W tiffs. I solved it by changing the image array values from 0, 255 to True, False.

src = <your B&W tiff file>
dst = <output filename>

# Open PIL.Image
img = Image.open(src)

# Change img values
a = np.array(img)
a = np.array([x == 255 for x in a])
img = Image.fromarray(a)

# Save Out Compressed Tiff
img.save(dst, compression='group4')

Upvotes: 1

Gary Hughes
Gary Hughes

Reputation: 4510

Just got bitten by this myself so I thought I'd share my finds despite the question being a few months old.

The tiff_jpeg compression refers to the "old-style" JPEG-encoded TIFF files and is now obsolete. Using compression='jpeg' instead of compression='tiff_jpeg' worked for me.

The available options for compression can currently be found in the COMPRESSION_INFO dict at https://github.com/python-pillow/Pillow/blob/master/PIL/TiffImagePlugin.py. The tiff_lzw compression also isn't mentioned in the docs but worked for creating an LZW encoded TIFF for me using Pillow 3.4 on Windows.

Upvotes: 2

Related Questions