Convert PNG to Biton TIFF

-Edit- FYI .. I am converting b & w documents scanned as grayscale or color.

1) The first solution worked, it just changed to black and white (black background, white text). It also took about 10 minutes.
2) The JAI solution in the second answer did not work for me. I tried this before posting here.

Has anyone worked with other libraries for free, or handled image manipulation for free?

-Original- I am trying to convert PNG to bitonal TIFF using Java ImageIO. Did anyone manage to do this? I got it to convert from PNG to TIFF. I'm not sure if I need to convert the BufferedImage (PNG) that I was reading or converting to TIFF as I write it. I have searched and searched but nothing works? Anyone have any suggestions where to look?

Here is the code that converts ...

public static void test() throws IOException {

    String fileName = "4848970_1";
    // String fileName = "color";
    String inFileType = ".PNG";
    String outFileType = ".TIFF";

    File fInputFile = new File("I:/HPF/UU/" + fileName + inFileType);
    InputStream fis = new BufferedInputStream(new FileInputStream(fInputFile));
    ImageReaderSpi spi = new PNGImageReaderSpi();
    ImageReader reader = spi.createReaderInstance();
    ImageInputStream iis = ImageIO.createImageInputStream(fis);
    reader.setInput(iis, true);
    BufferedImage bi = reader.read(0);

    int[] xi = bi.getSampleModel().getSampleSize();

    for (int i : xi) {
        System.out.println("bitsize " + i);
    }

    ImageWriterSpi tiffspi = new TIFFImageWriterSpi();
    TIFFImageWriter writer = (TIFFImageWriter) tiffspi.createWriterInstance();

    // TIFFImageWriteParam param = (TIFFImageWriteParam) writer.getDefaultWriteParam();
    TIFFImageWriteParam param = new TIFFImageWriteParam(Locale.US);
    String[] strings = param.getCompressionTypes();
    for (String string : strings) {
        System.out.println(string);
    }

    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType("LZW");

    File fOutputFile = new File("I:\\HPF\\UU\\" + fileName + outFileType);
    OutputStream fos = new BufferedOutputStream(new FileOutputStream(fOutputFile));
    ImageOutputStream ios = ImageIO.createImageOutputStream(fos);

    writer.setOutput(ios);
    writer.write(null, new IIOImage(bi, null, null), param);

    ios.flush();
    writer.dispose();
    ios.close();

}

      

I tried changing the compression to type "CCITT T.6" as it looks like what I want, but I get the error "Bits per sample must be 1 for T6 compression!" Any suggestion would be appreciated.

+1


source to share


2 answers


Chances are you need something like this to convert to 1 bit before saving to TIFF with CCITT compression.



Extract a bit - remember that converting from a different bit depth to 1 bit is non-trivial. You are performing a data reduction operation, and there are dozens of domain-specific solutions that vary widely in output quality (blind threshold, adaptive threshold, anti-aliasing, local threshold, global threshold, etc.). None of them are particularly good for all types of images (adaptive threshold is good for documents, but lousy for photos, for example).

+2


source


As the cap said, you need to do the transformation, Java won't magically do it for you ... If the PNG image is already black and white (as it seems from looking at your comment), using a threshold is probably the best solution.



Someone seems to have the same problem: HELP: how to compress the tiff . The solution is set in a stream (untested!).

0


source







All Articles