jruby / image_voodoo

ImageScience-compatible image processing for JRuby
MIT License
26 stars 10 forks source link

Save as JPEG with quality? #4

Closed PetrKaleta closed 10 years ago

PetrKaleta commented 10 years ago

It seems there's no way to set quality when saving jpegs. Which is quite important option, i think. Based on results its default value is 70% which is producing quite bad looking images, especially when processing photos....

enebo commented 10 years ago

Oh yes. Amazing I have not thought of this before. In order to do this the Java APIs need to be capable of it. It would be weird if they did I think but Java can be weird like that...

PetrKaleta commented 10 years ago

Check this out http://www.java2s.com/Code/Java/2D-Graphics-GUI/WritesanimagetoanoutputstreamasaJPEGfileTheJPEGqualitycanbespecifiedinpercent.htm

PetrKaleta commented 10 years ago

Or here http://www.programcreek.com/java-api-examples/index.php?api=java.awt.image.BufferedImage code example 24.

enebo commented 10 years ago

Excellent. This should not be too hard to add.

PetrKaleta commented 10 years ago

Question is, how its on GAE endpoint

enebo commented 10 years ago

Do you need for the GAE backend or are you just trying to be complete. I would hope GAE would also have something for reducing size/quality of jpegs as part of their API.

PetrKaleta commented 10 years ago

Nope, just to be complete... currently I am writing that code, will past awt code here in few mins

enebo commented 10 years ago

https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/images/OutputSettings

PetrKaleta commented 10 years ago

or are you gonna add it? Covering both? Maybe we can call it simply def save_as_jpeg(file, quality) what do you think?

enebo commented 10 years ago

This is a tough question surprisingly. This is a fairly literate API so perhaps .quality(0.75).save which implies needing to save 0.75 as a side-effect so that when save happens it does it's thing. An explicit save like you are saying can also work, but it is more complicated if you factor in with_bytes (and I am unsure if there are any other persisting or data feed variants) as it would also need quality.

If you want to take a crack at this then great. Otherwise I can do it.

PetrKaleta commented 10 years ago

Hmmm, good point with that quality method but I think its a bit above my skills. Right now I am trying to create that custom save_as_jpeg method (as I need it today), but honestly its my first time rewriting this awt code to ruby, so fingers crossed :)

When do you think you can look on it?

PetrKaleta commented 10 years ago

Here is my try, works surprisingly well

class ImageVoodoo
  java_import javax.imageio.ImageIO
  java_import javax.imageio.IIOImage
  java_import javax.imageio.ImageWriteParam
  java_import javax.imageio.stream.FileImageOutputStream

  # Save image as JPEG with given quality
  def save_as_jpg(file, quality)
    fail(ArgumentError, 'Quality out of range') if quality < 0 || quality > 1

    iter = ImageIO.getImageWritersByFormatName('jpg')
    writer = iter.next

    iwp = writer.getDefaultWriteParam
    iwp.setCompressionMode(ImageWriteParam::MODE_EXPLICIT)
    iwp.setCompressionQuality(quality)

    output = FileImageOutputStream.new(JFile.new(file))

    writer.setOutput(output)
    writer.write(nil, IIOImage.new(@src, nil, nil), iwp)

    true
  ensure
    writer.dispose if writer
    output.close if output
  end
end
enebo commented 10 years ago

@PetrKaleta I just landed quality method in commit 2df831a. You can see usage in samples/lossy.rb. Can you do a checkout and see if this is working for you? If so I will spin a release.

enebo commented 10 years ago

Oh also thanks for the snippet. It made adding this functionality much simpler.

PetrKaleta commented 10 years ago

Will test it now, thanks!

btw: you can also mention this quality method in readme sample...

PetrKaleta commented 10 years ago

Works like a charm!

enebo commented 10 years ago

great. I will update example and put out a new release. Thanks for reporting a glaring missing feature.

enebo commented 10 years ago

Released!