0

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

4

4 回答 4

0
BufferedImage bufferedImage = ImageIO.read(new File("input file directory...image.png"));

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

这应该采用缓冲图像(jpeg、png...等)并将其正确转换为 pgm。

编辑:可以在http://www.oracle.com/technetwork/java/index.html找到允许 .pgm 文件与 ImageIO 一起使用的 JAI 插件

于 2015-05-27T18:49:24.533 回答
0

这是我找到的一个例子。未测试。

// Create the OutputStream.
OutputStream out = new FileOutputStream(fileToWriteTo);
// Create the ParameterBlock.
PNMEncodeParam param = new PNMEncodeParam();
param.setRaw(true.equals("raw"));
//Create the PNM image encoder.
ImageEncoder encoder = ImageCodec.createImageEncoder("PNM", out, param);

请参阅编写 PNM 文件

于 2015-05-28T07:30:58.437 回答
0

我找到了答案。我已经将外部 JAI imageio 添加到我的库中。

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

它应该说“pnm”而不是“pgm”。新文件将自动具有 pgm 扩展名。

于 2015-06-01T17:00:09.677 回答
0

也许晚了,但我只写了一个。一个简单的 PGM 写入器,采用值在 [0.0,1.0] 范围内的 2d-double 数组。

    public static void WritePGM(string fileName, double[,] bitmap)
    {
        var width = bitmap.GetLength(0);
        var height = bitmap.GetLength(1);
        var header = "P5\n" + width + " " + height + "\n255\n";
        var writer = new BinaryWriter(new FileStream(fileName, FileMode.Create));
        writer.Write(System.Text.Encoding.ASCII.GetBytes(header));
        for (var j = 0; j < height; j++)
        {
            for (var i = width-1; i >= 0; i--)
            {
                var c = (byte)(System.Math.Max(System.Math.Min(1.0, bitmap[i, j]), 0.0) * 255.0);
                writer.Write(c);
            }
        }
        writer.Close();
    }
于 2016-03-03T20:47:07.733 回答