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

4

19

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

Abdominal answered 31/8, 2012 at 13:27 Comment(1)
I had the same problem with png compression. Switching to scikit-image and using its imsave worked wonders.Wrongheaded
P
8

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.

Poundage answered 31/8, 2012 at 13:57 Comment(2)
I have added the image that I am working with. Considering your answer - I realize that there are programs that will allow me to decrease png's size but I would like to use OpenCVs' functionality.Abdominal
You provided a JPEG file instead of the original PNG, so I can't really look at the compression. But other than that: You will not get better compression via OpenCV. One last thing may be that you store 16bit images instead of 8bit. This happens if you use a CV_16U matrix.Poundage
C
7

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

Carnarvon answered 26/5, 2014 at 9:42 Comment(0)
R
2

Semi-related, but I had the same issue with matplotlib.image.imsave - it would save an 8-bit grayscale image as 16-bit, which ballooned the size, even after using scipy.misc.bytescale to make sure it was an 8-bit array. However, scipy.misc.imsave saved it correctly as an 8-bit image.

Renege answered 6/8, 2015 at 4:35 Comment(1)
From scipy docs: imsave is deprecated! imsave is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imwrite instead.Botello
T
0

You can use a third-party command line tool optipng to re-compress and shrink the png file size without losing anything.

Reference:

http://optipng.sourceforge.net/pngtech/optipng.html

https://github.com/johnpaulada/optipng

Timoshenko answered 7/12, 2018 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.