thumbnailator icon indicating copy to clipboard operation
thumbnailator copied to clipboard

Resizing an index-paletted transparent PNG results in a black background

Open GoogleCodeExporter opened this issue 10 years ago • 23 comments

I want to resize a png file in following code:
  Thumbnails.of(srcImg).size(width, height).toFile(optImg)

But all transparent background turns to black.
See the attach
What should I do?

thumbnailator-0.4.0 in sun/oracle jdk1.7

Thank you very much!

Original issue reported on code.google.com by [email protected] on 8 Nov 2012 at 4:27

Attachments:

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Hi,

Thank you for reporting this issue.

I was able to reproduce the issue you have encountered with Thumbnailator 0.4.2.

The root cause is that the original image is a image which is using an indexed 
palette with a specific color used for the transparent pixels. (Similar to how 
transparent GIFs work)

There is no workaround for this issue (as fixing this problem will require 
additional checks in the code), so there isn't anything that can be done at 
this moment.

If it is possible, if you can use a PNG image with an actual alpha 
(transparency) channel, then it should be able to keep the transparency 
correctly.

Original comment by [email protected] on 11 Nov 2012 at 11:56

  • Changed title: Resizing an index-paletted transparent PNG results in a black background
  • Changed state: Accepted
  • Added labels: Priority-Low
  • Removed labels: Priority-Medium

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Thank you for your help.
Sincerely

Original comment by [email protected] on 13 Nov 2012 at 9:25

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

I would love to see this resolved since I can't control whether the source 
image has an alpha channel or not.

Let me know if there's anything I can do.

Original comment by [email protected] on 13 Feb 2013 at 10:33

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Use the following with your image to replace the Transparent Pixels with White 
color.  It worked perfectly with me.

if( buffered.getColorModel().getTransparency() != Transparency.OPAQUE) {
            int w = buffered.getWidth();
            int h = buffered.getHeight();
            image2 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image2.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,w,h);
            g.drawRenderedImage(buffered, null);
            g.dispose();
        }
Thanks..

Original comment by [email protected] on 25 Feb 2013 at 9:44

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

[deleted comment]

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

This happens also with GIFs.

Original comment by [email protected] on 3 Mar 2013 at 9:41

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

I have verified that the background will turn black for both PNG and GIF files 
(which have a transparent color set in their indexed pallete) using 
Thumbnailator 0.4.3 running Java 7.

I will increase the priority of this issue, as it appears to affect more users 
than I had initially expected.

I would like to thank everyone who has taken their time to provide input toward 
this issue.

Original comment by [email protected] on 7 Apr 2013 at 8:15

  • Added labels: Priority-Medium
  • Removed labels: Priority-Low

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Plz let the user choose what color to use as background when pngs with 
transparency are converted to jpg

Original comment by [email protected] on 14 May 2013 at 6:31

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

I would also like to be able to choose what color is used to replace the 
transparency.

Original comment by [email protected] on 4 Dec 2013 at 9:31

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

I have exactly the same problem. Do we have any update on this?

Original comment by [email protected] on 12 Aug 2014 at 11:25

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Hi guys,

is there any update on this? I ran into the same problem. Would also like to 
have an easy way to configure the resulting background color out of a 
transparent image.

Original comment by [email protected] on 9 Sep 2014 at 5:00

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

HI

I just ran into this issue and cant seem to find a workaround. 

E.g if I have a PNG and do this 
File fn = new File("test.png") ;
File f = new File("test-small.png)" ;
int s = 300 ;
Thumbnails.of(fn).size(s, s).toFile(f);

The resultant file has a back background when displayed


Original comment by [email protected] on 17 Mar 2015 at 11:33

Attachments:

  • [blackwire-c310-mono (1).png](https://storage.googleapis.com/google-code-attachments/thumbnailator/issue-41/comment-12/blackwire-c310-mono %281%29.png)
  • 3487-300.png

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Hello there,

Yes, I can confirm that the newly uploaded image also exhibits the same issue 
as the image originally attached to this issue. (The image of the star)

The issue with this new image (headset) is also the same as the original image 
(star) in that they are both index-paletted PNGs which specifies a specific 
color to be transparent.

Original comment by [email protected] on 18 Mar 2015 at 5:25

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

Hi,
I'm developing an application where many users will be submitting a screenshot 
taken from their computer.  We are receiving a complaint that all screenshots 
taken from Mac saved as PNG files are resized with color distortion.  Is this 
the same problem reported on this issue?  Please see attached files for 
original file and thumbnail generated.  Is there any workaround or fix for this?
Thank you in advance.

Original comment by [email protected] on 16 Apr 2015 at 3:57

Attachments:

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

I tried to resizing the image on Java 6 (Linux) with Thumbnailator 0.4.8, and 
was unable to reproduce the issue.

Which environment (OS, Java vendor/version) and version of Thumbnailator are 
you using?

Also, could you provide the code that is being used to invoke Thumbnailator?

The code doesn't need to be exact -- I would like to know what type of input 
(`BufferedImage`? `File`?) and output (`BufferedImage`? `File`?) you are 
working with.

Original comment by [email protected] on 22 Apr 2015 at 1:29

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

This issue is also affecting me!
A fix would be appreciated

Original comment by [email protected] on 14 May 2015 at 6:56

GoogleCodeExporter avatar Jul 25 '15 05:07 GoogleCodeExporter

+1: We will also like to have this feature.

jforjava1981 avatar Aug 20 '15 15:08 jforjava1981

4 years since this issue was firstly reported and no solution. Clearly thumbnailator developers don't care about this issue that's affecting lots of people. Thank you!

ClintEsteMadera avatar Jul 28 '16 16:07 ClintEsteMadera

Is there something that's wrong with the option suggested here for solving this issue: https://stackoverflow.com/questions/44383332/how-to-replace-black-background-with-white-on-image-resizing-using-thumbnailator

ie: call .addFilter(new Canvas(...., Color.WHITE))

It seems to work, is there a reason why shouldn't use this?

theyuv avatar Aug 30 '17 14:08 theyuv

@theyuv, it's not clear whether the linked Stack Overflow question is using a transparent PNG that's index-paletted. This issue was filed regarding a PNG image that was using index-palette transparency.

If the original image is a PNG with an alpha channel, then the answer in the linked Stack Overflow question would work, if what you're trying to accomplish is to add a solid color background.

coobird avatar Aug 30 '17 15:08 coobird

@coobird Thanks for the response and the clarification. It worked for my png file but I didn't check whether or not it was index-paletted.

theyuv avatar Aug 31 '17 10:08 theyuv

I have found a solution for this, might help others. Please test and verify.

I replaced below code:

BufferedImage finalImage = Thumbnails.of(temp)
                        .size(videoWidth, videoHeight)                    
                        .asBufferedImage();

with:

BufferedImage finalImage = Thumbnails.of(temp)
                        .size(videoWidth, videoHeight)                        
                        .imageType(BufferedImage.TYPE_INT_ARGB)
                        .asBufferedImage();

Input image used was indexed 8 bit png with transparency.

ahtabbasi avatar Sep 19 '17 07:09 ahtabbasi

I have tried many PNG configuration, with GIMP, and I don't seem to have it working, even with the Alpha Channel. Someone has a sample PNG with transparency that works for them ? I am using the latest lib also from maven

simonguertin avatar May 01 '20 20:05 simonguertin