Search code examples
javasavebufferedimagejaipgm

How do I save a BufferedImage to a pgm file?


I looked through the internet but I could not find an answer. I have a pgm file which I use as a BufferedImage to do a convolution (I use JAI for that) but I am having trouble in saving it back to a pgm file. So far I used following code to save:

JAI.create("filestore", newImage, outputFileName);

With that I get a pgm file but when I open the image IfranView tells me that it is a TIF file with incorrect extension. What do I need to change?

I appreciate any help! Please provide code examples if possible. Thanks everyone.

Kind regards, Staniel


Solution

  • I found the answer. I already added the external JAI imageio to my library.

    ImageIO.write(bufferedImage, "pnm", new File("output file directory.....image.pgm"));
    

    Instead of "pgm" it should say "pnm". The new file will automatically have the pgm extension.