Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color gets dull: opencv cv2.imread cv2.imwrite

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

like image 673
Riken Mehta Avatar asked Jun 15 '17 08:06

Riken Mehta


People also ask

Does cv2 Imwrite overwrite?

imwrite will overwrite existing files without outputting an error or asking for confirmation. Image of any format can be saved using this method.

Is cv2 Imread RGB or BGR?

OpenCV uses BGR image format. So, when we read an image using cv2. imread() it interprets in BGR format by default. We can use cvtColor() method to convert a BGR image to RGB and vice-versa.

Why is cv2 Imread not working?

If you are receiving a NoneType error and your code is calling cv2. imread , then the likely cause of the error is an invalid file path supplied to cv2.

Does cv2 Imwrite compress?

This is because cv2. imwrite() has the ability to compress the image while writing it into the file.


1 Answers

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
like image 152
Mark Setchell Avatar answered Oct 18 '22 19:10

Mark Setchell