Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV imwrite increases the size of png image

I am doing image manipulation on the png images. I have the following problem. After saving an image with imwrite() function, the size of the image is increased. For example previously image is 847KB, after saving it becomes 1.20 MB. Here is a code. I just read an image and then save it, but the size is increased. I tried to set compression params but it doesn't help.

Mat image;
image = imread("5.png", -1); 

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
compression_params.push_back(0);

imwrite("output.png",image,compression_params);

What could be a problem? Any help please. Thanks.

like image 529
Karmar Avatar asked Dec 04 '12 14:12

Karmar


People also ask

What does CV Imwrite do?

imwrite() Saves an image to a specified file. The function imwrite saves the image to the specified file.

Does cv2 Imwrite compress?

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

What does cv2 Imwrite return?

Image written to file-system : True. cv2. imwrite() returned true which means the file has been successfully written to the path specified.

Does 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. The format of the image is defined by the filename's extension ( . jpg , .


2 Answers

PNG has several options that influence the compression: deflate compression level (0-9), deflate strategy (HUFFMAN/FILTERED), and the choice (or strategy for dynamically chosing) for the internal prediction error filter (AVERAGE, PAETH...). It seems OpenCV only lets you change the first one, and it hasn't a good default value for the second. So, it seems you must live with that.

Update: looking into the sources, it seems that compression strategy setting has been added (after complaints), but it isn't documented. I wonder if that source is released. Try to set the option CV_IMWRITE_PNG_STRATEGY with Z_FILTERED and see what happens

See the linked source code for more details about the params.

like image 167
leonbloy Avatar answered Sep 29 '22 01:09

leonbloy


@Karmar, It's been many years since your last edit.

I had similar confuse to yours in June, 2021. And I found out sth which might benefit others like us.

PNG files seem to have this thing called mode. Here, let's focus only on three modes: RGB, P and L. To quickly check an image's mode, you can use Python:

from PIL import Image
print(Image.open("5.png").mode)

Basically, when using P and L you are attributing 8 bits/pixel while RGB uses 3*8 bits/pixel. For more detailed explanation, one can refer to this fine stackoverflow post: What is the difference between images in 'P' and 'L' mode in PIL?

Now, when we use OpenCV to open a PNG file, what we get will be an array of three channels, regardless which mode that file was saved into. Three channels with data type uint8, that means when we imwrite this array into a file, no matter how hard you compress it, it will be hard to beat the original file if it was saved in P or L mode.

I guess @Karmar might have already had this question solved. For future readers, check the mode of your own 5.png.

like image 36
phunc20 Avatar answered Sep 29 '22 03:09

phunc20