Gaudard
Gaudard

Reputation: 149

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!

Upvotes: 11

Views: 2536

Answers (3)

Zolt&#225;n Nagy
Zolt&#225;n Nagy

Reputation: 1

I encountered this problem though.

I have a monochrome tif file and when I tried to save it under a different name, I got the same error. I supplemented the code with this a statement

new_image = image.convert('1')

and it has been working ever since.

from tkinter import Tk, Canvas, Button, Label
from tkinter import filedialog
from PIL import ImageTk, Image
root = Tk()
root.title('My first GUI')
canvas = Canvas(root, width = 300, height = 300)      
canvas.pack()

def open():
    global my_image
    
    root.filename=filedialog.askopenfilename(initialdir="", title="Hola", filetypes=(("All files","*.*"),("tiff","*.tiff")))
    my_label=Label(root, text= root.filename).pack()
    image = Image.open(root.filename)
    new_filename = str(root.filename+'_.tif')
    print(new_filename)
    new_image = image.convert('1') #new line before saving
    new_image.save(new_filename)
    new_image = image.resize((1024, 768))
    my_image=ImageTk.PhotoImage(new_image)
    my_image_label=Label(image=my_image).pack()

my_btn=Button(root, text="Open File", command=open).pack()

root.mainloop()

Monocrome tif file:

new_image = image.convert('1')

Colorful tif file:

new_image = image.convert("RGBA")

Upvotes: 0

Niclas
Niclas

Reputation: 21

Had the same issue, when using PIL to combine multiple images to one and adding a label. I could fix this easily by converting the .tif file to a .png file in MS Paint (pls don't hate me for using MS :D). Quality of the final merged image was not reduced.

Upvotes: -1

Chris Sprance
Chris Sprance

Reputation: 302

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.

Upvotes: 1

Related Questions