2012-10-15 56 views
13

當我使用PIL==1.1.7加載TIFF圖像,它似乎得到使用的色彩錯誤:Python PIL錯誤地解碼TIFF顏色(使用不正確的顏色空間)?

bad colorspace conversion

這是怎麼回事?

  • .tiff使用convert test.jpg test.tiff創建(但似乎與其他TIFF文件也發生)
  • 它可以在這裏找到:http://hul.wolever.net/dump/test.tiff
  • 輸出時PIL.Image.DEBUG = True
 
>>> from PIL import Image 
>>> Image.DEBUG = True 
>>> Image.open("/tmp/test.tiff").show() 
tag: ImageWidth (256) - type: short (3) - value: (560,) 
tag: ImageLength (257) - type: short (3) - value: (401,) 
tag: BitsPerSample (258) - type: short (3) - value: (8, 8, 8) 
tag: Compression (259) - type: short (3) - value: (7,) 
tag: PhotometricInterpretation (262) - type: short (3) - value: (2,) 
tag: FillOrder (266) - type: short (3) - value: (1,) 
tag: DocumentName (269) - type: ascii (2) - value: /tmp/lemur.tiff 
tag: StripOffsets (273) - type: long (4) - value: (8, 9282, 18712, 28312, 38000, 48730, 59052, 70406, 83010, 95978, 108967, 121029, 133136, 145555, 157411, 168289, 179433, 191157, 202954, 214664, 226914, 238919, 250547, 261871, 273282, 284453) 
tag: Orientation (274) - type: short (3) - value: (1,) 
tag: SamplesPerPixel (277) - type: short (3) - value: (3,) 
tag: RowsPerStrip (278) - type: short (3) - value: (16,) 
tag: StripByteCounts (279) - type: long (4) - value: (9274, 9430, 9600, 9688, 10730, 10322, 11354, 12604, 12968, 12989, 12062, 12107, 12419, 11856, 10878, 11144, 11724, 11797, 11710, 12250, 12005, 11628, 11324, 11411, 11171, 2541) 
tag: XResolution (282) - type: rational (5) - value: ((1207959552, 16777216),) 
tag: YResolution (283) - type: rational (5) - value: ((1207959552, 16777216),) 
tag: PlanarConfiguration (284) - type: short (3) - value: (1,) 
tag: ResolutionUnit (296) - type: short (3) - value: (1,) 
tag: PageNumber (297) - type: short (3) - value: (0, 1) 
tag: JPEGTables (347) - type: undefined (7) - value: ????C?? 

???}!1AQa"q2??#B??R??$3br? 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz??????????????????????????????????????????????????????????????????????????? 
tag: YCbCrSubSampling (530) - type: short (3) - value: (2, 2) 
*** Summary *** 
- compression: jpeg 
- photometric_interpretation: 2 
- planar_configuration: 1 
- fill_order: 1 
- size: (560, 401) 
format key: ('II', 2, 1, 1, (8, 8, 8),()) 
- raw mode: RGB 
- pil mode: RGB 
+6

您得到一個+1僅僅是爲了向我介紹'Image.DEBUG'。 –

+0

在我看來,原始來源是一個問題。我嘗試了你的形象,並得到相同的結果。但是,嘗試使用另一個tiff或運行jpg轉換會產生預期結果。 – sberry

+1

@sberry。 tiff不是一個單一的格式。它更像是一個靈活的容器,可以利用多種壓縮和圖像類型。我認爲這可能不是文件本身的問題,因爲我的桌面圖像查看程序可以按預期顯示文件。 –

回答

9

這很可能是由於您的TIFF圖像包含由Adobe Photoshop生成的壓縮JPEG數據,該數據使用特殊標記來指示e正確的色彩空間。我猜PIL不知道這個標記(至少在TIFF嵌入的JPEG中),所以它假定圖像在YCbCr色彩空間(我們可以在截圖中看到)。

查看詳細的答案here,我相信這正是你的情況。

可能的解決方案是將JPEG轉換爲其他方法(無壓縮的JPEG數據)。例如,如果PIL設法正確打開您的JPEG,您可以用PIL重寫TIFF:

from PIL import Image 
img = Image.open("test.jpg") 
tif = Image.new("RGB", img.size) 
tif.paste(img) 
tif.save('result.tiff') 
+0

OP指出圖像是使用'convert'工具在命令行上創建的。據我瞭解這個問題,我認爲問題是讓PIL讀取轉換後的tiff文件,沒有找到更好的轉換方法。 –

+0

@Steven,我相信沒有快速的方法讓PIL正確讀取這個文件。我在'TiffImagePlugin.py'的TIFF閱讀過程中嘗試僞造圖像模式,但無濟於事。底層的PIL二進制文件以相同的方式解碼嵌入的JPEG文件。我很抱歉,我忍不住更好。 –