Grayscale Image

Convert Image into Black and White using Java Cloud SDK

Raster images are produced on daily basis via mobile phones, scanners etc and they are comprised of RGB color scheme. Now with RBG color, the image size is large and on the other end, a grayscale image is compressed image as its one of the types in which the value of each pixel is a single sample representing only one amount of light; that is, it carries only intensity information. Furthermore, a grayscale image is a black-and-white or gray monochrome image consisting entirely of shades of gray. Contrast ranges from black, the weakest intensity, to white, the strongest. So in this article, we are going to develop grayscale image converter to modify the exposure of your images to shades of gray. Adjust the gray tone intensity of the image as desired with grayscale filter to eliminate colorful distractions and emphasize the meaning of your designs.

Grayscale Image Conversion API

We have REST based API offering the capabilities to manipulate images file in the Cloud. It also supports the feature to convert image into black and white. So in order to develop Grayscale image converter using Java, we are going to use Aspose.Imaging Cloud SDK for Java. Apart from conversion to grayscale image, you may also convert source image to variety of other supported formats. Now in order to get started with conversion operations, the first step is to add its reference in java project by including following information in pom.xml (maven build type project).

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-imaging-cloud</artifactId>
        <version>22.4</version>
    </dependency>
</dependencies>

The next step is to obtain the client credentials from Cloud Dashboard and if you do not have an account over Aspose Cloud Dashboard, please create a free account through valid email address. Now login using newly created account and lookup/create Client ID and Client Secret at Aspose Cloud Dashboard.

Grayscale Photo using Java

This section explains the details on how to load an existing image and convert to grayscale picture. Please following the instructions specified below to accomplished the requirement.

  • The first step is to create an instance of ImagingApi based on personalized client credentials
  • Secondly, read the JPG file form local folder using File object
  • Thirdly, create byte[] instance to read file using readAllBytes(…) method
  • The next step is to create an instance of CreateGrayscaledImageRequest which requires Byte array and resultant grayscale image name
  • Finally, call the method createGrayscaledImage(…) to generate grayscale photo and resultant file is stored on cloud storage
grayscale image

Convert Image into Black and White

Grayscale Picture

Resultant Grayscale photo

Generate Grayscale Picture using cURL Commands

We can also convert Image into Black and White using cURL commands. Since our APIs are only accessible to authorized users, so in order to access the APIs using cURL commands over command line terminal, we need to first generate a JWT access token (based on client credentials) using following command.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now that we have JWT token, please call GrayscaleImage API to produce grayscale picture. Once the grayscale photo has been generated, the resultant image is returned in response stream.

curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/image1.jpg/grayscale" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o "grayscale.jpg"

Conclusion

This article has provided all the details on how we can convert Image into Black and White using Java Cloud SDK. Similarly, we have also explored the option to accomplish the requirement to generate grayscale picture using cURL commands. Other than these options, you may quickly explore API features within web browser via swagger API Reference. We highly suggest to explore Product Documentation to learn more about other exciting features offered by the API.

Furthermore, all our Cloud SDKs are developed under MIT license so the complete source code can be downloaded from GitHub. Lastly, in case you encoutner any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.

Please visit the following links to learn more about: