使用 ImageIO 从具有 Alpha 透明度的图像中保存具有索引透明度的 GIF
我有一个具有 alpha 透明度的 BufferedImage,我需要将其保存为具有索引透明度的 GIF。没有半透明像素,因此应该可以进行转换。
使用 http://gman.eichberger.de/ 下找到的代码2007/07/transparent-gifs-in-java.html,我定义了一个透明颜色(例如绿色,它不是当前图像的一部分)并使其透明。工作正常,但它混合了颜色表,所有颜色看起来都很糟糕(尽管我只使用 3 种不同的颜色)。
有没有一种方法可以调整这一点,或者有另一种方法可以将此类 ARGB 图像转换为索引图像,而不会造成明显的质量损失?
我的图像的构建方式:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
// Some more painting here
graphics.dispose();
感谢您的帮助!
I have a BufferedImage with alpha transparency, which I need to save as GIF with index transparency. There are no semi-opague pixels, therefore a conversion should be possible.
Using the code found under http://gman.eichberger.de/2007/07/transparent-gifs-in-java.html, I define a transparency color (green, for instance, which is not part of the current image) and make it transparent. Works fine, BUT it mixes up the color table and all the colors look awful (although I only use 3 different colors).
Is there a way to adjust this or else another way of converting such an ARGB-Image to an indexed one without significant quality loss?
The way my image gets constructed:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
// Some more painting here
graphics.dispose();
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ImageIO 处理透明度尤其是保存为 GIF 图像时是一场噩梦。对于不同的图像格式,甚至相同的图像格式但不同的变体,ImageIO 中的透明度支持是不完整的。
如果你已经有一个二进制透明的 BufferedImage,我有一个颜色量化工具和一个 GIF 编码器< /a> 将其另存为具有透明度的 GIF。如果图像的实际颜色小于 256,则不会应用量化。否则,颜色将减少到小于 256,但保留透明颜色。可以在量化过程中应用抖动,因此生成的图像尺寸可能比平常更大。
下面的屏幕截图显示了两张图像:左边一张是具有 Alpha 通道透明度的原始 PNG 图像,而右边一张是从同一 PNG 图像转换而来的透明 GIF 图像。 GIF 图像实际上减少为 128 色,但质量很棒。 (PNG:49K,GIF:17K)
图片来源:http://svg2rlg.googlecode.com/svn-history/r2/trunk/test-suite/png/butterfly.png
Using ImageIO to handle transparency especially when saving as a GIF image is a nightmare. Transparency support in ImageIO is incomplete for different image formats and even same image format but different variations.
If you already has a binary transparent BufferedImage, I've got a color quantization tool and a GIF encoder to save it as GIF with transparency. If the actual colors of the image are less than 256, no quantization will be applied. Otherwise, colors will be reduced to less than 256 but the transparent color is retained. Dithering can be applied during quantization process, so the resulting image size might be larger than usual.
The following screenshot shows two images: the one to the left is the original PNG image with alpha channel transparency while the right hand side one is a transparent GIF image converted from the same PNG image. The GIF image is actually reduced to 128 colors but the quality is great. (PNG: 49K, GIF: 17K)
Image source: http://svg2rlg.googlecode.com/svn-history/r2/trunk/test-suite/png/butterfly.png