颜色变钝:opencv cv2.imread cv2.imwrite

时间:2017-06-15 08:41:00

标签: python opencv image-processing cv2

我正在使用opencv模块来读写图像。这是代码,下面是我正在阅读的图像,第二个图像是在使用cv2.imwrite()将其保存在磁盘上之后。

import cv2

img = cv2.imread('originalImage.jpg')
cv2.imwrite('test.jpg',img)

original image

image saved using opencv

第二张图像中的颜色很暗,非常明显。有没有解决这个问题的方法,或者我在某种设置参数上缺少..?

3 个答案:

答案 0 :(得分:3)

The difference is that the initial image (on the left in the diagram) has an attached ICC profile whereas the second one (on the right) does not.

enter image description here

I obtained the above image by running the ImageMagick utility called identify like this:

identify -verbose first.jpg    > 1.txt
identify -verbose second.jpg   > 2.txt

Then I ran the brilliant opendiff tool (which is part of macOS) like this:

opendiff [12].txt

You can extract the ICC profile from the first image also with ImageMagick like this:

convert first.jpg profile.icc

答案 1 :(得分:3)

我对@mark提出的关于ICC简介的观点进行了一些研究。我已经找到了在python PIL模块中处理这个问题的方法。这是适合我的代码。我还学会了使用PNG文件格式而不是JPEG来进行无损转换。

import Image
img = Image.open('originalImage.jpg')
img.save('test.jpg',icc_profile=img.info.get('icc_profile'))

我希望这对其他人也有帮助。

答案 2 :(得分:0)

您的第一个输入图像在元数据中有一些关联的icc-Profile,这是一个可选属性,大多数设备可能不会首先注入它。 ICC配置文件基本上执行一种颜色校正,并在校准期间为每个唯一设备计算校正系数。

现代Web浏览器,图像查看实用程序主要在将图像渲染到屏幕之前考虑此ICC配置文件信息,这就是两个图像中存在差异的原因。

但不幸的是,OpenCV没有从图像的元数据中读取ICC配置来执行任何颜色校正。

相关问题