Free Support Forum - aspose.com

Binarize a color image to a bw image

Simplified problem:
I would like to create a 1bit bw image from a color image. To do that I use the Bradley’s method.

RasterImage ri = (RasterImage)RasterImage.load("C:\\temp\\in.png");
ri.binarizeBradley(threshold);
ri.save("C:\\temp\\out.png");

Unfortunately the out.png has a bit depth of 24 instead of 1.

Actual problem:
I have a BufferedImage of type BufferedImage.TYPE_INT_RGB and would like to binarize it.
The resulting BufferedImage should be of type BufferedImage.TYPE_BYTE_BINARY.

public BufferedImage binarize(BufferedImage rgbImage) {
	RasterImage rasterImage = ImageExtensions.fromJava(rgbImage);
	rasterImage.binarizeBradley(8.0);
	BufferedImage result = ImageExtensions.toJava(rasterImage);
	return result;
}

The resulting BufferedImage has type BufferedImage.TYPE_INT_ARGB instead of BufferedImage.TYPE_BYTE_BINARY

@martin.duerig

Can you please share the source PNG file along with generated output PNG file so that we may analyze that on our end to assist you further.

You can download the files here (available till December 25, 2020).
https://filebin.net/bpgt4okbiv4o47rd

I provided three input und three output examples with different source bit depths

  • 8 bit depth. Input: in8bit.png, output: out8bit.png
  • 24 bit depth. Input: in24bit.png, output: out24bit.png
  • 32 bit depth. Input: in32bit.png, output: out32bit.png

As already mentioned, none of the output pictures results in a bit depth of 1.

@martin.duerig

I have added a ticket with ID IMAGINGJAVA-1972 in our issue tracking system to further investigate and resolve the issue on our ed. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

@martin.duerig

Can you please try using the following code on your ent to be able to save to Grayscale PNG 1,2,4,8 bits. We have attached them in attached zip file for reference.

using (Aspose.Imaging.FileFormats.Png.PngImage image =
(Aspose.Imaging.FileFormats.Png.PngImage)Image.Load(@"in8bit.png")
)
{
image.BinarizeBradley(10);
image.Save(
@"out_binarize1bit.png",
new Aspose.Imaging.ImageOptions.PngOptions()
{
Progressive = false,
ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.Grayscale,
BitDepth = (byte)1
});
}

1_8_bit_png_output.zip (1.3 MB)