9

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')
cv2.imwrite('test.jpg',img)

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..?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Riken Mehta
  • 171
  • 11

3 Answers3

4

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 = Image.open('originalImage.jpg')
img.save('test.jpg',icc_profile=img.info.get('icc_profile'))

I hope this will help others as well.

Riken Mehta
  • 171
  • 11
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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • So how do i take care of this ICC profile while writing the image through opencv..? – Riken Mehta Jun 15 '17 at 09:40
  • Nice catch! However this does not solve OPs problem! – Ander Biguri Jun 15 '17 at 13:00
  • 1
    Hi @AnderBiguri Yes, I agree it doesn't to *solve* the problem but I believe that answers that help understand what the problem is are perfectly acceptable and if two folks each know half an answer... well, we may be able to work out a full answer between us. For the moment, I am going to say that I believe **OpenCV** is more biased towards solving "computer vision problems" and is less skewed towards worrying about the fidelity of colour management for print and reproduction purposes so I suspect OP will need to take account of the ICC profile himself. I may be wrong :-) – Mark Setchell Jun 15 '17 at 14:26
  • @RikenMehta OpenCV doesn't read or write ICC profiles (or other metadata) so you'll need to look outside of OpenCV for your solution. – SSteve Jun 15 '17 at 14:47
  • @SSteve i have to make a montage of images and write it back to disk for later use of visuals. So can you suggest me some other modules in python that i can use..? – Riken Mehta Jun 15 '17 at 15:23
  • @RikenMehta Sorry. I know OpenCV but not Python image management modules. – SSteve Jun 15 '17 at 17:42
  • @RikenMehta You can try using the PIL (python imaging library) module. – cs95 Jun 15 '17 at 18:51
0

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.

ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • do you have idea about any utility that takes care of ICC profile..? In any language and in any framework.? – Riken Mehta Jun 16 '17 at 03:07
  • I Don't guess if there is any library that supports reading image with meta data, but you may look at this [link](https://imageoptim.com/color-profiles.html) to get images in sRGB profile rather than ICC. – ZdaR Jun 16 '17 at 03:10