0

我想应用图像数据中的 alpha 蒙版。我有不同格式的图像,即 tiffs、PSD PNG 和 jpeg。我将它们作为缓冲图像读取,并希望使用 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)
    }
}
4

1 回答 1

0

使用该Paths.readClipped(...)方法将读取图像数据和 Photoshop 剪切路径资源,并将剪切路径应用于图像。换句话说,BufferedImage根据路径,结果将包含透明度。

如果您喜欢分别读取路径和图像,可以使用实用程序类的readPath(...)applyClippingPath(...)方法:Paths

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 通道。

于 2020-08-06T12:37:20.377 回答