How to Convert an Image to Black-and-White Grayscale using Java

Removing color from an image both reduces the complexity of the file and creates a the familiar “black & white” aesthetic effect. With our Grayscale API, you can easily convert your images to black-and-through a simple color removal operation and return the encoding to create your new file. It’s free & easy to use — just register a free account on our website (free accounts come with a limit of 800 API calls per month: perfect for small-scale projects & startups) and follow the instructions included below to structure your API call in Java.

Our first task is to install the API client. We can get that started by including a reference in the pom.xml repository:


We can wrap up installation by adding another reference to the pom.xml dependency:


Now we can call the color removal function. First include the imports at the top of your file, then pass your image file path through the imageFile parameter:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.FilterApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
FilterApi apiInstance = new FilterApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
byte[] result = apiInstance.filterBlackAndWhite(imageFile);
} catch (ApiException e) {
System.err.println("Exception when calling FilterApi#filterBlackAndWhite");

Now just make sure to include your API key where shown in the code comments, and you’re all set to go!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store



There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.