Grayscale an Image Introduction
Grayscale method allows to turn all colors of an image to a shades of gray
There are two APIs to crop an image:
The first API expects you to first upload an image to Cloud Storage then pass its name in the API URL. After updating the image parameters, the API returns the updated image in the response. If you would like to save the updated image on the Cloud Storage, you explicitly need to do this as shown in the below examples.
On the other hand, with the second API, you can directly pass the image in the request body. It also lets you save the updated image on the Cloud Storage by specifying the outPath parameter value. However, if you do not specify the value, the response contains a streamed image.
Please check Common Operations Format Support Map to know the supported conversion formats.
Resource URI
Aspose.Imaging Cloud APIs Swagger UI lets you call REST APIs directly from the browser. The description of the API and its parameters are also given there.
cURL Example
SDKs
Using an SDK (API client) is the quickest way for a developer to speed up the development. An SDK takes care of a lot of low-level details of making requests and handling responses and lets you focus on writing code specific to your particular project. Check out our GitHub repository for a complete list of Aspose.Imaging Cloud SDKs along with working examples, to get you started in no time. Please check Available SDKs article to learn how to add an SDK to your project.
SDK Examples
.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to aspose-imaging-cloud-dotnet/Examples/AsposeImagingCloudSdkExamples
public void GrayscaleImageFromStorage()
{
// Input formats could be one of the following:
// BMP, GIF, DJVU, WMF, EMF, JPEG, JPEG2000, PSD, TIFF, WEBP, PNG, DICOM, CDR, CMX, ODG, DNG and SVG
string sampleImageFileName = "GrayscaleSampleImage.bmp";
// Upload local image to Cloud Storage
using (var localInputImage = File.OpenRead(Path.Combine(ExampleImagesFolder, sampleImageFileName)))
{
var uploadFileRequest = new UploadFileRequest(Path.Combine(CloudPath, imageName), image);
var result = ImagingApi.UploadFile(uploadFileRequest);
}
string folder = CloudPath; // Input file is saved at the Examples folder in the storage
string storage = null; // We are using default Cloud Storage
var request = new GrayscaleImageRequest(sampleImageFileName, folder, storage);
using (Stream updatedImage = this.ImagingApi.GrayscaleImage(request))
{
// Save updated image to local storage
using (var fileStream = File.Create(Path.Combine(ExampleImagesFolder, "GrayscaleSampleImage_out.bmp")))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
}
Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void GrayscaleImageFromStorage() throws Exception {
String fileName = "GrayscaleSampleImage.bmp";
byte[] inputImage = Files.readAllBytes(Paths.get(ExampleImagesFolder, fileName));
UploadFileRequest request = new UploadFileRequest(Paths.get(CloudPath, fileName).toString(), image, null);
FilesUploadResult response = ImagingApi.uploadFile(request);
String folder = CloudPath; // Input file is saved at the Examples folder in the storage
String storage = null; // We are using default Cloud Storage
GrayscaleImageRequest request = new GrayscaleImageRequest(getSampleImageFileName(), folder, storage);
byte[] updatedImage = ImagingApi.grayscaleImage(request);
Path path = Paths.get(OutputFolder, "GrayscaleSampleImage_out.bmp").toAbsolutePath();
Files.write(path, updatedImage);
}
.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-imaging-cloud/aspose-imaging-cloud-dotnet/tree/master/Examples
public void CreateGrayscaledImageFromRequestBody()
{
// Input formats could be one of the following:
// BMP, GIF, DJVU, WMF, EMF, JPEG, JPEG2000, PSD, TIFF, WEBP, PNG, DICOM, CDR, CMX, ODG, DNG and SVG
string sampleImageFileName = "GrayscaleSampleImage.bmp";
using (FileStream inputImageStream = File.OpenRead(Path.Combine(ExampleImagesFolder, SampleImageFileName))
{
String outPath = null; // Path to updated file (if this is empty, response contains streamed image).
String storage = null; // We are using default Cloud Storage
var request = new CreateGrayscaledImageRequest(inputImageStream, outPath, storage);
using (Stream updatedImage = this.ImagingApi.CreateGrayscaledImage(request))
{
// Save updated image to local storage
using (var fileStream = File.Create(Path.Combine(ExampleImagesFolder, "GrayscaleSampleImage_out.bmp")))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
}
}
Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void CreateGrayscaledImageFromRequestBody() throws Exception {
String fileName = "GrayscaleSampleImage.bmp";
String storage = null; // We are using default Cloud Storage
String outPath = null; // Path to updated file (if this is empty, response contains streamed image)
byte[] inputStream = Files.readAllBytes(Paths.get(ExampleImagesFolder, fileName));
CreateGrayscaledImageRequest request = new CreateGrayscaledImageRequest(inputStream, outPath, storage);
byte[] updatedImage = ImagingApi.createGrayscaledImage(request);
byte[] updatedImage = ImagingApi.grayscaleImage(request);
Path path = Paths.get(OutputFolder, "GrayscaleSampleImage_out.bmp").toAbsolutePath();
Files.write(path, updatedImage);
}