Free Support Forum - aspose.com

Converting any images to pure, binary black and white images

So far I have been unable to find any detailed documentation regarding binarizeFixed method. What I have found is the following:

"Parameters:
threshold - Threshold value. If corresponding gray value of a pixel is greater than threshold, a value of 255 will be assigned to it, 0 otherwise."

I understand this to mean any pixel below 100 will be black (0) and any pixel above this number will be white (255) however, when running the following line of code:

rasterCachedImage.binarizeFixed((byte) 127);

That is not the case. There are still a lot of gray pixels left in the image result.

Can you please explain how this function works in more detail and recommend how I can get an image to convert to a true black and white image using a threshold value like 127?

Here is the code we are testing with:


try {

        // Load an image in an instance of Image
        Image image = Image.load("PATH TO INPUT FILE");
        
        double scale = 0;
        int width = image.getWidth();
        scale = 50 / (double) width;

        //Reduce image size for analysis
        image.resizeWidthProportionally((int)(width * scale), ResizeType.LanczosResample);

        // Cast the image to RasterCachedImage
        RasterCachedImage rasterCachedImage = (RasterCachedImage) image;

        // Check if image is cached
        if (!rasterCachedImage.isCached()) {
            // Cache image if not already cached
            rasterCachedImage.cacheData();
        }
        
        // Binarize image with pre defined fixed threshold
        rasterCachedImage.binarizeFixed((byte) 127);

        // Save the resultant image
        rasterCachedImage.save("PATH TO OUTPUT FILE");

    } catch (Exception e) {

        e.printStackTrace();

    } 

Original File:
wolverine.jpg (83.1 KB)

Output File:
wolverine_small.jpg (1.9 KB)

@bryan.reynolds,

I have worked with image file shared by you and have been able to observe the issue specified. We need to investigate this on our end. An issue with ID IMAGINGJAVA-795 has been added as enhancement in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed.

@bryan.reynolds,

We have investigated the sample code and there is no error. You get this because you use JPEG as final format, BUT JPEG could not store pure image, anyway during JPEG compressing some artifacts are added in compressed image. Please, notice that online tool returned the PNG file, not JPEG.

If you want to get pure B/W image, please, use different image format as result.

Following are some examples:

Storing Black and White BMP

// Load an image in an instance of Image
Image image = Image.load("wolverine.jpg");

try
{
double scale;
int width = image.getWidth();
scale = 50 / (double) width;

//Reduce image size for analysis
image.resizeWidthProportionally((int)(width * scale), ResizeType.LanczosResample);

// Cast the image to RasterCachedImage
RasterCachedImage rasterCachedImage = (RasterCachedImage) image;

// Check if image is cached
if (!rasterCachedImage.isCached()) {
    // Cache image if not already cached
    rasterCachedImage.cacheData();
}

// Binarize image with pre defined fixed threshold
rasterCachedImage.binarizeFixed((byte) 127);

checkBw(image, "First test");

// Save the resultant B/W image
rasterCachedImage.save("wolverine-bw.bmp", new BmpOptions());

}
finally
{
image.dispose();
}

Store Black and White PNG

// Load an image in an instance of Image
Image image = Image.load("wolverine.jpg");

try
{
double scale;
int width = image.getWidth();
scale = 50 / (double) width;

//Reduce image size for analysis
image.resizeWidthProportionally((int)(width * scale), ResizeType.LanczosResample);

// Cast the image to RasterCachedImage
RasterCachedImage rasterCachedImage = (RasterCachedImage) image;

// Check if image is cached
if (!rasterCachedImage.isCached()) {
    // Cache image if not already cached
    rasterCachedImage.cacheData();
}

// Binarize image with pre defined fixed threshold
rasterCachedImage.binarizeFixed((byte) 127);

checkBw(image, "First test");

// Save the resultant B/W image
rasterCachedImage.save("wolverine-bw.png", new PngOptions());

}
finally
{
image.dispose();
}