bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.59k stars 1.59k forks source link

PNG images are not getting compressed correctly #1462

Open saketsneha opened 4 years ago

saketsneha commented 4 years ago

I m trying to compress the PNG images , and have tried to use different COMPRESSION algorithm , but the final image size is greater than original size. Image Used- https://upload.wikimedia.org/wikipedia/commons/d/d7/Astro_4D_stars_proper_radial_g_b_8mag_big.png

Before resize length 1966656 After resize length IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY - 6355730 After resize length IMWRITE_PNG_STRATEGY_FILTERED - 6355730 After resize length IMWRITE_PNG_STRATEGY_FIXED 6355730 After resize length IMWRITE_PNG_STRATEGY_RLE 6355730 The resizing(decreasing the dimension) is increasing the size of the image after the first log.

Steps to reproduce I am using Java for this-

resizeByThird(sourceImage: Path, destinationImage: Path) {
        val sourceImageMat = imread(sourceImage.toString())
        val resizedImage = Mat()
        resize(sourceImageMat, resizedImage, Size(3840, 2208))
        println(sourceImage.toFile().length())
        val params: IntArray = intArrayOf(IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY)
        imwrite(destinationImage.toString(), resizedImage, params)
        println(destinationImage.toFile().length())
    }

I am facing this issue with JPEG image as well

I have also tried to use IMWRITE_PNG_COMPRESSION for PNG ,even that did not help

saudet commented 4 years ago

Is this a duplicate of https://github.com/opencv/opencv/issues/17718 ?

saketsneha commented 4 years ago

Yes.I was told by openCv contributor that they do not support JavaCV , so thought this plateform might help

saudet commented 4 years ago

It looks like Leptonica supports encoding to PNG with a palette: https://tpgit.github.io/Leptonica/pngio_8c_source.html So you might want to try that instead: http://bytedeco.org/javacpp-presets/leptonica/apidocs/org/bytedeco/leptonica/global/lept.html#pixWriteStreamPng-org.bytedeco.javacpp.Pointer-org.bytedeco.leptonica.PIX-float-