Question

I am resizing png images which might have alpha channel.

Everything works good, with one exception: I get some gray pixels around the transparent areas.

The original image doesn't have any drop shadows.

Is there a way to fix this / work it around?

wrong alpha value

I am using SmoothResize by Gustavo Daud (See the first answer to this question), to resize the png image.

I cannot provide the code that I am using as I did not write it and do not have the author's permission to publish it.

Was it helpful?

Solution

I suspect that is caused by 2 things: funny RGBA values in PNG and naive resizing code.

You need to check your PNG contents. You are looking for RGB values in transparent areas. Despite transparent areas having Alpha at 0, they still have an RGB info. In your case I would expect that transparent areas are filled with black RGB value. That is what might cause the grey outline after resize if the resize is done naively. Example: What happens if code resizes 2 adjustent pixels (0,0,0,0) and (255,255,255,255) into one? Both pixels contribute by 50% The result is 128,128,128,128) which is semi-transparent grey. Same thing happens when you upscale by e.g x1.5, the added pixel inbetween original two will be grey. Usually that does not happen because image-editing software is smart enough to fill those invisible pixels with color from nearest visible pixel.

You can try to "fix" the PNG by filling transparent areas with white (or another color that is on border of your images).

Another approach is to use an advanced resizing code (write or find library), that will ignore transparent pixels RGB values (e.g. by taking RGB from closest non-transparent pixel).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top