coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.16k stars 787 forks source link

Thumbnailator corrupts PNG image #144

Closed richiksc closed 4 years ago

richiksc commented 5 years ago

Expected behavior

Resize an image to a smaller size without greatly affecting the visual quality of it.

My source image is this: git-logo

and when I run it through Thumbnailator using

Thumbnails.of(image)
  .size(128, 128)
  .keepAspectRatio(true)
  .outputFormat("png")
  .toFile(file);

I expect it to come out as a 128x128 PNG image retaining its transparent background - the file is smaller and there may be some artifacting, but no major aberrations.

Actual behavior

The output image is this: logo-1 thumbnail

The logo has lost its transparent background for a black one and there are horrible red vertical 'scanlines' running through the orange part of the logo. For some reason, this issue only happens on this image and not the 5 other images I have ran through Thumbnailator. The only difference is that this source image is an 8bit png instead of a 24bit png.

Steps to reproduce the behavior

Run the above sample image through the provided Thumbnailator code..

Environment

rntrp commented 5 years ago

Without going too much into detail, have you tried setting the image type to ARGB?

Thumbnails.of(image)
  .size(128, 128)
  .keepAspectRatio(true)
  .outputFormat("png")
  .imageType(ThumbnailParameter.DEFAULT_IMAGE_TYPE) // a.k.a. BufferedImage.TYPE_INT_ARGB
  .toFile(file);
coobird commented 4 years ago

@richiksc As you've noticed, it happens for paletted 8-bit PNGs, and the solution posted by @rntrp solution would work.

This is actually a duplicate of #41 which happens for index-paletted transparent PNGs.