Ask Your Question

Not loosing the quality of pictures saved with cv2.imwrite()

asked 2015-03-09 06:20:00 -0600

begueradj gravatar image

I am wondering seriously about the effects of cv2.imwrite() function of OpenCV.

I noticed that when I read pictures with cv2.imread() and save them again with cv2.imwrite() function, their quality is not the same any more for the human eyes.

I ask you how can I keep the quality of the image the same as the original after saving it using cv2.imwrite() function.

I ask this question because I have really a serious issue in a larger program and when I checked the quality of the pictures saved by this function, I guesses that my problem comes certainly from this function.

For example, I draw using the mouse movements small red (Red=255) squares on picture . When I save the picture and count the number of pixels that have Red color equal to 255 I get very few of them only even if I draw a lot of them in pure red color. But when I check the image by my eyes, I notice the red pixels I drawed are not correctly saved in the correct red color I chosed (255).

Any one does know how to resolve this problem ? I mean to save the pictures using OpenCV without degrading its quality.

edit retag flag offensive close merge delete


@kbarni thank you very much, that is the right answer indeed

begueradj gravatar imagebegueradj ( 2015-03-09 07:39:16 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-09 06:51:07 -0600

kbarni gravatar image

JPG is a lossy file format, you will loose in image quality every time you save the image - even if normally it's not very visible for the human eye.

If you need a lossless format, use PNG or TIFF instead. It's easy, just change the file extension: use imwrite("test.png"); isntead of imwrite("test.jpg");

edit flag offensive delete link more


very nice @kbarni. I am really glad when I see people knowing the subject of image processing/computer vision .::thumps_up::.

theodore gravatar imagetheodore ( 2015-03-09 08:46:13 -0600 )edit

Question Tools

1 follower


Asked: 2015-03-09 06:20:00 -0600

Seen: 8,273 times

Last updated: Mar 09 '15