Closed PetrKaleta closed 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...
Or here http://www.programcreek.com/java-api-examples/index.php?api=java.awt.image.BufferedImage code example 24.
Excellent. This should not be too hard to add.
Question is, how its on GAE endpoint
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.
Nope, just to be complete... currently I am writing that code, will past awt code here in few mins
or are you gonna add it? Covering both? Maybe we can call it simply def save_as_jpeg(file, quality)
what do you think?
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.
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?
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
@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.
Oh also thanks for the snippet. It made adding this functionality much simpler.
Will test it now, thanks!
btw: you can also mention this quality method in readme sample...
Works like a charm!
great. I will update example and put out a new release. Thanks for reporting a glaring missing feature.
Released!
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....