使用ImageIO将具有索引透明度的GIF保存到具有Alpha透明度的图像中 [英] Save a GIF with index transparency using ImageIO out of an image with alpha transparency

查看:360
本文介绍了使用ImageIO将具有索引透明度的GIF保存到具有Alpha透明度的图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有alpha透明性的BufferedImage,我需要将其另存为具有索引透明性的GIF。没有半模糊像素,因此应该可以进行转换。



使用在


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!

解决方案

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

这篇关于使用ImageIO将具有索引透明度的GIF保存到具有Alpha透明度的图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆