How to Remove Transparency from an Image using Java

Cloudmersive
2 min readSep 13, 2022

One easy way to remove transparency from an image is to apply an opaque white background behind the original image. That’s exactly what our Transparency Removal API does, otherwise rendering the image at the same resolution & in the same file format.

The best part? You can use this API for free (up to 800 API calls per moth) by registering a free account on our website & using the code examples provided below to install the API client & structure your API call in Java.

We can begin installing the client by adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And we can wrap it up by adding a reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Next, we can include the following imports at the top of our file:

// 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.EditApi;

And finally, we can call the function, passing through our input file path & API key where indicated in the comments:

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)
//Apikey.setApiKeyPrefix("Token");
EditApi apiInstance = new EditApi();
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.editRemoveTransparency(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditApi#editRemoveTransparency");
e.printStackTrace();
}

After that, you’re all done — no more coding required. Easy as pie!

--

--

Cloudmersive

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