Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify compression quality #5

Open
ggrossetie opened this issue Mar 17, 2014 · 2 comments
Open

Allow to specify compression quality #5

ggrossetie opened this issue Mar 17, 2014 · 2 comments

Comments

@ggrossetie
Copy link

When you're using Scalr with a compressed image the result is much bigger.
Somehow the compression is lost.

For example when I scale down a 750_500px compressed image (65Kb) to a 740_500px image, the result is an 800Kb image 😢

val iter = ImageIO.getImageWritersByFormatName(ext)
val writer = iter.next()
val iwp = writer.getDefaultWriteParam
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
// Set compression quality
iwp.setCompressionQuality(.7F)
val output = new FileImageOutputStream(tmp)
writer.setOutput(output)
val iioImage = new IIOImage(resized, null, null)
writer.write(null, iioImage, iwp)
writer.dispose()
output.close()

Reference: http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java

Can we add a parameter compressionQuality (with default value 1.0F) to getRes https://github.com/digiPlant/play-scalr/blob/master/app/se/digiplant/scalr/api/Scalr.scala#L65 ?

A better way would be to preserve the compression quality of the image... maybe http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageWriteParam.html#MODE_COPY_FROM_METADATA

@ghost
Copy link

ghost commented Mar 17, 2014

I just want to point out real quickly that imgscalr (Scalr class) only operates on BufferedImage instances which are pure, uncompressed image data -- the library itself has nothing to do with image compression, that is handled (typically) by the built in Java encoding libraries that most people access via the ImageIO library.

That said, I am not sure what Play-scalr is doing; if it is actually reading and writing the compressed images, then I can understand this request and I redact my comment :)

@ggrossetie
Copy link
Author

@thebuzzmedia Yes you're right

The code in question is inside Play-scalr. The method resize() is actually writing the image after Imgscalr have done its job : https://github.com/digiPlant/play-scalr/blob/master/app/se/digiplant/scalr/api/Scalr.scala#L101

Imgscalr operates on uncompressed image data and we don't apply compression so we can have bigger image when we scale down... which is a little weird 😄

A better way would be to preserve the compression quality of the image... maybe http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageWriteParam.html#MODE_COPY_FROM_METADATA

It's a bad idea because the fact that the image is compressed is not always in metadata.
I think we should also surround this code with try {} catch {} because the compression is not always possible. With .png I get a Caused by: java.lang.UnsupportedOperationException: Compression not supported.

    try {
      val iter = ImageIO.getImageWritersByFormatName(ext)
      val writer = iter.next()
      val iwp = writer.getDefaultWriteParam
      iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
      iwp.setCompressionQuality(.7F)
      val output = new FileImageOutputStream(tmp)
      writer.setOutput(output)
      val iioImage = new IIOImage(resized, null, null)
      writer.write(null, iioImage, iwp)
      writer.dispose()
      output.close()
      tmp
    } catch {
      case e:UnsupportedOperationException =>
        val tmp = File.createTempFile(Random.nextString(20), ext)
        ImageIO.write(resized, ext.toUpperCase, tmp)
        tmp
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant