Getting clip paths from tif,jpg and psd images

259 Views Asked by At

(Please find the code snapshot here)-> 1. Hello so i have some tif and psd images that have clipping paths configured. what I want to do is, to extract them from java. so I use twelvemonkeys library that @haraldk has designed. but i am not able to get the paths. from the readPath() function. that is because, the imageinputstream that i get from the bufferedimage the buffer is all zeroes. i dont know why it happens. the buffer is valid untill ImageIO.createImageInputStream(source).(please refer the code snapshot in the link below). but after createimageinputstream(), the buffer has all zeroes. one more thing, this only works for jpg and tiffs. but for psd images i am not even getting the ByteArrayOutputStream as imageIO does not support psd images. can any one plz help me? thank you. the code snapshot is in the link below

1

There are 1 best solutions below

0
Harald K On

I have no idea what all that code in your screenshot does, as it is incomplete, but: You need to create the ImageInputStream from the original file contents. It's (IMO) quite straightforward. :-)

Either, read the image with the path applied:

try (ImageInputStream stream = ImageIO.createImageInputStream(new File("image_with_path.jpg")) {
    BufferedImage image = Paths.readClipped(stream);

    // Do something with the clipped image...
}

Or read the image and path separately (this is basically what readClipped(stream) does:

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...
}

I think your code does not work because you first read the image (using some non-disclosed code), then write only the pixel data to a temporary stream, and try to get the paths from there. But there are no path information in the BufferedImage, so the path information is "lost" in this operation. Again, you need to get it from the original file contents.