coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.14k stars 785 forks source link

Support progressive encoding for output (i.e. allow writing progressive JPEGs) #114

Open jlerbsc opened 7 years ago

jlerbsc commented 7 years ago

Hi, Is there a way to generate progressive jpeg with thumbnailator? Thanks,

coobird commented 7 years ago

No, Thumbnailator does not expose the functionality of the Java Image I/O API to specify whether to use progressive encoding. As that functionality is not exposed, is it not possible at this time to create progressive JPEGs.

That said, I'd be interested to hear how much interest there is. If there's a lot of interest, then I think it's worth incorporating this as a new feature.

coobird commented 6 years ago

Note to self:

API Design memo for a potential way to specify the output parameters of the compression format used for the output.

JPEG:

Thumbnails.of(images)
    .withOptions(OutputFormat.JPEG.usingProgressive().withQuality(0.9))
    .toFiles(Rename.PREFIX_DOT_THUMBNAIL);

PNG:

Thumbnails.of(images)
    .withOptions(OutputFormat.PNG.usingDefaults())
    .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
lopcode commented 6 years ago

This would be quite useful for a project I'm working on, as it's pretty content-heavy, and the UX for slow connections would be better with progressive images I think. https://www.adoptanimals.io/

Thanks for all your hard work so far, I did quite a bit of searching for good JVM thumbnail making and this one was by far the easiest to use and produced really nice results (all the animal images on that site are produced using it) 👍

coobird commented 6 years ago

@CarrotCodes I'm glad to hear that Thumbnailator is being put to good use, and thank you for your comment. :)

I can't make any promises when Thumbnailator will add support for setting progressive JPEGs, but I'll keep it in mind for features to work on. (I might end up creating a temporary workaround if I can't cleanly integrate it into the API yet.)

reisi007 commented 5 years ago

hi :) Is there any way we can assist you in implementing this? IMHO as a workaround we could get a bufferedImage and do the writing ourselves, so not a blocker for me

axelfontaine commented 3 years ago

In case this helps, here is the code I use for this (the example is in Kotlin, but it should be easy to translate it to Java):

ImageIO.createImageOutputStream(stream).use { imageOutputStream ->
    val writer = ImageIO.getImageWritersBySuffix("jpeg").next()
    try {
        writer.output = imageOutputStream
        writer.write(
            null,
            IIOImage(builder.asBufferedImage(), null, null),
            writer.defaultWriteParam.apply {
                progressiveMode = ImageWriteParam.MODE_DEFAULT
            }
        )
    } finally {
        writer?.dispose()
    }
}