Extract Frame from a Multi-Frame TIFF Image Introduction
This article explains how to get a frame from an existing TIFF image. The API also lets you resize, crop and rotateFlip the extracted frame. Moreover, the boolean parameter saveOtherFrames let you specify whether the result will include all other frames or just a specified frame.
The frame can be extracted using one of the following two APIs:
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 call Upload File API 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.
Resource URI
With Swagger UI you can call Aspose.Imaging REST APIs directly from the browser. Description of an API and its parameters are also provided there.
cURL Examples
SDK Source
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 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
public void GetImageFrameFromStorage()
{
String fileName = "Sample.tiff";
// Upload local image to Cloud Storage
using (FileStream localInputImage = File.OpenRead(ImagingBase.PathToDataFiles + fileName))
{
var uploadFileRequest = new UploadFileRequest(fileName, localInputImage);
FilesUploadResult result = this.ImagingApi.UploadFile(uploadFileRequest);
}
int? frameId = 1; // Number of a frame
int? newWidth = 300;
int? newHeight = 450;
int? x = 10;
int? y = 10;
int? rectWidth = 200;
int? rectHeight = 300;
string rotateFlipMethod = "Rotate90FlipX";
// Result will include just the specified frame
bool? saveOtherFrames = false;
string folder = null; // Input file is saved at the root of the storage
string storage = null; // We are using default Cloud Storage
GetImageFrameRequest getImageFrameRequest = new GetImageFrameRequest(fileName, frameId, newWidth, newHeight,
x, y, rectWidth, rectHeight, rotateFlipMethod, saveOtherFrames, folder, storage);
Stream imageFrame = this.ImagingApi.GetImageFrame(getImageFrameRequest);
// Save updated image to local storage
using (var fileStream = File.Create(ImagingBase.PathToDataFiles + "SingleFrame_out.tiff"))
{
imageFrame.Seek(0, SeekOrigin.Begin);
imageFrame.CopyTo(fileStream);
}
}
Java
This file contains 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
String APP_KEY = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String APP_SID = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String DATA_PATH = "src/main/resources/";
ImagingApi imagingApi = new ImagingApi(APP_KEY, APP_SID);
String fileName = "Sample.tiff";
try {
// Upload local image to Cloud Storage
File inputFile = new File(Main.DATA_PATH + fileName);
FileInputStream localInputImageStream = new FileInputStream(inputFile);
byte[] localInputImage = IOUtils.toByteArray(localInputImageStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(fileName, localInputImage, null);
FilesUploadResult result = imagingApi.uploadFile(uploadFileRequest);
Integer frameId = 1; // Number of a frame
Integer newWidth = 300;
Integer newHeight = 450;
Integer x = 10;
Integer y = 10;
Integer rectWidth = 200;
Integer rectHeight = 300;
String rotateFlipMethod = "Rotate90FlipX";
// Result will include just the specified frame
Boolean saveOtherFrames = false;
String outPath = null; // Path to updated file (if this is empty, response contains streamed image).
String folder = null; // Input file is saved at the root of the storage
String storage = null; // We are using default Cloud Storage
GetImageFrameRequest getImageFrameRequest = new GetImageFrameRequest(fileName, frameId, newWidth, newHeight,
x, y, rectWidth, rectHeight, rotateFlipMethod, saveOtherFrames, outPath, folder, storage);
byte[] updatedImage = imagingApi.getImageFrame(getImageFrameRequest);
// Save updated image to local storage
FileOutputStream fos = new FileOutputStream(Main.DATA_PATH + "SingleFrame_out.tiff");
fos.write(updatedImage);
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
.NET
This file contains 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
public void CreateImageFrameFromRequestBody()
{
string fileName = "Sample.tiff";
using (FileStream inputImageStream = File.OpenRead(ImagingBase.PathToDataFiles + fileName))
{
int? frameId = 1;
int? newWidth = 300;
int? newHeight = 450;
int? x = 10;
int? y = 10;
int? rectWidth = 200;
int? rectHeight = 300;
string rotateFlipMethod = "Rotate90FlipX";
bool? saveOtherFrames = false;
string outPath = null; // Path to updated file (if this is empty, response contains streamed image).
string storage = null; // We are using default Cloud Storage
CreateImageFrameRequest createImageFrameRequest = new CreateImageFrameRequest(inputImageStream, frameId, newWidth,
newHeight, x, y, rectWidth, rectHeight, rotateFlipMethod, saveOtherFrames, outPath, storage);
Stream imageFrame = this.ImagingApi.CreateImageFrame(createImageFrameRequest);
// Save updated image to local storage
using (var fileStream = File.Create(ImagingBase.PathToDataFiles + "SingleFrame_out.tiff"))
{
imageFrame.Seek(0, SeekOrigin.Begin);
imageFrame.CopyTo(fileStream);
}
}
}
Java
This file contains 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
String APP_KEY = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String APP_SID = ""; // Get AppKey and AppSID from https:dashboard.aspose.cloud/
String DATA_PATH = "src/main/resources/";
ImagingApi imagingApi = new ImagingApi(APP_KEY, APP_SID);
try {
File file = new File(Main.DATA_PATH + "Sample.tiff");
byte[] imageData = Files.readAllBytes(file.toPath());
Integer frameId = 1;
Integer newWidth = 300;
Integer newHeight = 450;
Integer x = 10;
Integer y = 10;
Integer rectWidth = 200;
Integer rectHeight = 300;
String rotateFlipMethod = "Rotate90FlipX";
Boolean saveOtherFrames = false;
String outPath = null; // Path to updated file (if this is empty, response contains streamed image).
String storage = null; // We are using default Cloud Storage
PostImageFrameRequest postImageFrameRequest = new PostImageFrameRequest(imageData, frameId, newWidth,
newHeight, x, y, rectWidth, rectHeight, rotateFlipMethod, saveOtherFrames, outPath, storage);
byte[] updatedImage = imagingApi.postImageFrame(postImageFrameRequest);
// Save updated image to local storage
FileOutputStream fos = new FileOutputStream(Main.DATA_PATH + "SingleFrame_out.tiff");
fos.write(updatedImage);
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}