Python PIL错误地解码TIFF颜色(使用不正确的颜色空间)? [英] Python PIL incorrectly decoding TIFF colors (using incorrect colorspace)?
问题描述
当我使用PIL==1.1.7
加载TIFF图像时,似乎得到了错误的色彩空间:
这是怎么回事?
-
.tiff
是使用convert test.jpg test.tiff
创建的(但似乎也与其他tiff文件一起发生) - 可以在以下位置找到它: http://hul.wolever.net/dump/test.蒂芙
-
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
最可能的原因是您的TIFF图像包含Adobe Photoshop生成的压缩JPEG数据,该数据使用特殊的标记来指示正确的色彩空间.我猜PIL不知道这个标记(至少在嵌入TIFF的JPEG中),因此它假定图像位于YCbCr色彩空间(我们可以在屏幕截图中看到).
在此处中查看详细答案,我相信这完全是您的情况. >
您可能的解决方案是用其他方法(没有压缩的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')
When I use PIL==1.1.7
to load TIFF images, it seems to get the colorspace wrong:
What's going on?
- The
.tiff
was created usingconvert test.jpg test.tiff
(but seems to happen with other tiff files too) - It can be found at: http://hul.wolever.net/dump/test.tiff
- The output when
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
This is most likely due to the fact that your TIFF images contain compressed JPEG data generated by Adobe Photoshop, that uses a special marker to indicate the correct colorspace. I guess PIL doesn't know this marker (at least, in a TIFF-embedded JPEG), so it assumes that the image is in YCbCr colorspace (which we can see on the screenshot).
See the detailed answer here, I believe it's exactly your case.
The possible solution for you is to convert your JPEGs with other methods (without compressed JPEG data). For example, if PIL manages to open your JPEGs correctly, you can rewrite the TIFF with PIL:
from PIL import Image
img = Image.open("test.jpg")
tif = Image.new("RGB", img.size)
tif.paste(img)
tif.save('result.tiff')
这篇关于Python PIL错误地解码TIFF颜色(使用不正确的颜色空间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!