OpenCV - imread(), imwrite() increases the size of png?


Question

I wanted to try out some simple operations on files and I started with opening and saving files (I use Python)

image = cv2.imread("image.png")
cv2.imwrite("image_processed.png", image)

After this operation my original image from 33kB transforms into the same looking 144kB image.

I have tried doing something like this : http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#imwrite

    params = list()
    params.append(cv.CV_IMWRITE_PNG_COMPRESSION)
    params.append(8)

    image = cv2.imread("image.png")
    cv2.imwrite("image_processed.png",image,params)

But this does not change much ( size decreased to 132kB )

This is the image which I am working with:

enter image description here

1
10
5/24/2014 10:04:37 AM

Accepted Answer

Some png writers like GIMP write much better compressed PNGs than standard libpng, which is used by opencv. You can also open and save the image again with Imagemagick, and see what difference that makes (as compared to OpenCV).

There is even specialized software that tries to better re-compress PNGs, like pngcrush.

Can you provide the image in question? I would like to play with it, regarding file size optimization.

6
8/31/2012 1:57:15 PM

As hinted by ypnos, your source file is jpg (even if it has the png extension). That is why, when you save it in png format, it will use more space, as you are changing the format (jpg to png).

Try replacing the last line with:

cv2.imwrite("image_processed.jpg",image,params)

And you will see that the size doesn't change that much.

Alternatively, keep the code as it is, but use a different image, such as http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon