Color in image gets dull after saving it in OpenCV


I am using opencv module to read and write the image. here is the code and below is the image i am reading and second image is after saving it on disk using cv2.imwrite().

import cv2

img = cv2.imread('originalImage.jpg')

original image

image saved using opencv

It is significantly visible that colors are dull in second image. Is there any workaround to this problem or I am missing on some sort of setting parameters..?

Asked By: Riken Mehta



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
Answered By: Mark Setchell

Your first input image has some icc-Profile associated in the meta-data, which is an optional attribute and most devices may not inject it in the first place. The ICC profile basically performs a sort of color correction, and the correction coefficients are calculated for each unique device during calibration.

Modern Web Browsers, Image Viewing utilities mainly take into account this ICC profile information before rendering the image onto the screen, that is the reason why there is a diff in both the images.

But Unfortunately OpenCV doesn’t reads the ICC config from the meta data of the image to perform any color correction.

Answered By: ZdaR

I have done a bit of research on the point @mark raised about ICC profile. I have figured out a way to handle this in python PIL module. here is the code that worked for me. I have also learned to use PNG file format rather JPEG to do lossless conversion.

import Image
img ='originalImage.jpg')'test.jpg','icc_profile'))

I hope this will help others as well.

Answered By: Riken Mehta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.