我想要应用图像数据中的alpha蒙版。我有不同格式的图像,即tiffs,PSD PNGand jpeg.I。我正在将它们读取为bufferedimage,并希望使用Twelvemonkeys来获得图像中配置的alpha路径,并相应地应用透明度。但是我找不到相关的函数。请帮帮忙。
ImageInputStream stream = ImageIO.createImageInputStream(new File(c:/img.psd);
BufferedImage image = Paths.readClipped(stream);
image.getcoclormodel().hasAlpha();
for(i < image.getwidth()) {
for(j < image.getHeight()) {
pixels = image.getRGB(i, j, width, height, null, 0, width);
Color col = new Color(pixels[pixelIndex]);
int p = col.getAlpha()
image.setRGB(i, j, width, height, p, 0, width)
}
}发布于 2020-08-06 20:37:20
使用Paths.readClipped(...)方法将读取图像数据和Photoshop剪切路径资源,并将剪切路径应用于图像。换句话说,生成的BufferedImage将根据路径包含透明度。
如果您希望分别读取路径和图像,则可以使用Paths实用程序类的readPath(...)和applyClippingPath(...)方法:
try (ImageInputStream stream = ImageIO.createImageInputStream(new File("image_with_path.jpg")) {
Shape clip = Paths.readPath(stream);
stream.seek(0);
BufferedImage image = ImageIO.read(stream);
if (clip != null) {
image = Paths.applyClippingPath(clip, image);
}
// Do something with the clipped image...
}上面的代码基本上做了与readClipped(...)相同的事情,但允许您检查和修改每个步骤。
PS:请注意,Paths中的方法仅用于包含具有ClippingPath资源(资源id为0x07d0)的Photoshop资源块的图像。图像的alpha通道将始终存在。对于包含多个alpha通道(也称为“透明蒙版”)的PSD和TIFF文件,目前无法访问额外的alpha通道。
https://stackoverflow.com/questions/62922463
复制相似问题