简体   繁体   English

在Delphi中调整png图像大小 - 不正确的Alpha通道

[英]Resize png image in Delphi - incorrect alpha channel

I am resizing png images which might have alpha channel. 我正在调整可能具有alpha通道的png图像。

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? 有没有办法解决这个/解决它?

错误的alpha值

I am using SmoothResize by Gustavo Daud (See the first answer to this question ), to resize the png image. 我正在使用Gustavo Daud的SmoothResize(请参阅此问题的第一个答案),以调整png图像的大小。

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. 我无法提供我正在使用的代码,因为我没有写它并且没有作者的许可发布它。

I suspect that is caused by 2 things: funny RGBA values in PNG and naive resizing code. 我怀疑这是由两件事引起的:PNG中的搞笑RGBA值和天真的大小调整代码。

You need to check your PNG contents. 您需要检查您的PNG内容。 You are looking for RGB values in transparent areas. 您正在寻找透明区域中的RGB值。 Despite transparent areas having Alpha at 0, they still have an RGB info. 尽管透明区域的Alpha为0,但它们仍然具有RGB信息。 In your case I would expect that transparent areas are filled with black RGB value. 在你的情况下,我希望透明区域充满黑色RGB值。 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? 示例:如果代码将2个调整像素(0,0,0,0)和(255,255,255,255)调整为一个,会发生什么? Both pixels contribute by 50% The result is 128,128,128,128) which is semi-transparent grey. 两个像素贡献50%。结果是128,128,128,128),这是半透明灰色。 Same thing happens when you upscale by eg x1.5, the added pixel inbetween original two will be grey. 当您通过例如x1.5升级时会发生同样的事情,原始两者之间的添加像素将是灰色的。 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). 您可以尝试通过填充白色(或图像边框上的其他颜色)的透明区域来“修复”PNG。

Another approach is to use an advanced resizing code (write or find library), that will ignore transparent pixels RGB values (eg by taking RGB from closest non-transparent pixel). 另一种方法是使用高级调整大小代码 (写入或查找库),其将忽略透明像素RGB值(例如,通过从最接近的非透明像素获取RGB)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM