Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 940
Patryk Avatar asked Aug 31 '12 13:08

Patryk


People also ask

What does Imread do in OpenCV?

imread() Loads an image from a file. The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

Can Imread read PNG?

imread also supports reading 16-bit-per-pixel data from BMP, TIFF and PNG files.

Can cv2 Imread read PNG?

The syntax of imread() function contains a second argument whose default value is cv2. IMREAD_COLOR. Any transparency present in the image is not read. To read PNG images with transparency (alpha) channel, use cv2.

What is the use of cv2 Imwrite?

cv2. imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.


1 Answers

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.

like image 146
ypnos Avatar answered Oct 11 '22 03:10

ypnos