【问题标题】:Identify colour space of any image if icc_profile is empty (PIL)如果 icc_profile 为空 (PIL),则识别任何图像的色彩空间
【发布时间】:2018-06-01 10:41:13
【问题描述】:
当我使用 PIL 阅读图像时,我有大量图像,其中许多图像的 icc_profile 为空。我检查 icc 配置文件的方式是:
from PIL import Image
img = Image.open('image.jpg')
icc = img.info.get('icc_profile', '')
即使 icc_profile 为空,是否有办法识别图像的色彩空间(最好使用 PIL)?
【问题讨论】:
标签:
python
image
image-processing
python-imaging-library
【解决方案1】:
除了在ICC profile 中查找色彩空间信息外,您还可以查看 EXIF 元数据标签。特别是EXIF标签ColorSpace(0xA001)等于1时表示sRGB。其他值不标准,根据this document,但可能表示其他颜色空间。另一个有用的 EXIF 标记可能是 InteropIndex (0x0001)。
你可以像这样检查这些标签:
from PIL import Image
img = Image.open('image.jpg')
def exif_color_space(img):
exif = img._getexif() or {}
if exif.get(0xA001) == 1 or exif.get(0x0001) == 'R98':
print ('This image uses sRGB color space')
elif exif.get(0xA001) == 2 or exif.get(0x0001) == 'R03':
print ('This image uses Adobe RGB color space')
elif exif.get(0xA001) is None and exif.get(0x0001) is None:
print ('Empty EXIF tags ColorSpace and InteropIndex')
else:
print ('This image uses UNKNOWN color space (%s, %s)' %
(exif.get(0xA001), exif.get(0x0001)))
此外,如果您的文件来自DCIM folder(例如数码相机或智能手机),Adobe RGB 颜色空间可以通过以下划线开头的名称(例如_DSC)或具有JPG 以外的扩展名(比如JPE)。
如果您的图像的色彩空间仍然未知,最安全的做法是假设为 sRGB。如果稍后用户发现图像看起来太暗或太暗,他们可以在其他颜色空间中查看图像,这可能会使图像看起来更饱和。