Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PIL modifying and then saving a TIFF returns error

TLDR; I'm trying to take a TIFF, resize it, then save it. However it returns an error. This works fine if I change the saved filetype to png or jpg.

System: Windows 7 Tried using both Python 3.4 and 2.7.

Code:

from PIL import Image

try:                                               #test file exists
    im = Image.open(r"c:\temp\file.tif")
except:
    print("Error opening image")

multiply = 5                                       #how much bigger
processing = tuple([multiply*x for x in im.size])  #maths
saved = (r"c:\temp\biggerfile.tif")               #save location

imB = im.resize((processing))                      #resizing

imB.save(saved)                                    #saving

I need to resize a TIFF because I'm using tesseract-ocr, and resizing the image to get a better output. The program seems to work best with a TIFF.

The error I receive is:

_TIFFVSetField: c:\temp\biggerfile.tif: Bad value 2 for "ExtraSamples" tag.
Traceback (most recent call last):
  File "step1.py", line 15, in <module>
    imB.save(saved)
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 1684, in save
    save_handler(self, fp, filename)
  File "C:\Python34\lib\site-packages\PIL\TiffImagePlugin.py", line 1185, in _save
    e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 430, in _getencoder
    return encoder(mode, *args + extra)
RuntimeError: Error setting from dictionary

Thanks!

like image 490
Gaudard Avatar asked Oct 28 '14 23:10

Gaudard


1 Answers

Try to install libtiff http://gnuwin32.sourceforge.net/packages/tiff.htm

File "C:\Python34\lib\site-packages\PIL\TiffImagePlugin.py", line 1185, in _save
e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)

Looks like that's the error that is holding you up. It's trying to access libtiff and you don't have it installed so it's failing.

like image 175
Chris Sprance Avatar answered Oct 05 '22 13:10

Chris Sprance