11
TLDR;我正在嘗試使用TIFF,調整其大小,然後保存它。但是它會返回一個錯誤。如果我將保存的文件類型更改爲png或jpg,此工作正常。使用PIL修改並保存TIFF返回錯誤
系統:Windows 7 嘗試使用Python 3.4和2.7。
代碼:
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
我需要調整一個TIFF因爲我使用的Tesseract-OCR和調整圖像大小,以獲得更好的輸出。該程序似乎最適用於TIFF。
我收到的錯誤是:
_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
謝謝!
此代碼適用於我。我唯一能想到的是要麼沒有適當的編碼器,要麼就是在'.tif'上絆倒。你有沒有嘗試將它改爲'.tiff'的文件名?在Linux上,'.tif'確實很好,但你永遠不知道。保存時發生錯誤,並且名爲'_getencoder'的方法試圖返回無效數據,所以我認爲它與文件名或編碼器有關。 – DuckPuncher 2014-10-28 23:38:29
我在Windows上這樣做,但我已經嘗試過3.4和2.7。我曾嘗試使用.tiff和沒有擴展名和「TIF」的文件名。 – Gaudard 2014-10-28 23:50:43
我認爲錯誤指向[unassociated alpha data](http://www.awaresystems.be/imaging/tiff/tifftags/extrasamples.html)。 PIL可以在加載(調整大小之前)作爲tif後立即重新保存圖像嗎?也許你試圖擺脫這個數據,絆倒PIL。 – 2014-10-29 01:01:52