AI Server incorporates various image processing capabilities. It wraps some common operations into easier-to-use endpoints, such as:
- Crop Image: Crop an image to a specific size.
- Convert Image: Convert an image to a different format.
- Scale Image: Scale an image to a different resolution.
- Watermark Image: Add a watermark to an image.
INFO
These operations are processed on the AI Server itself, rather than an external API or agent.
Using Image Endpoints​
These endpoints are used in a similar way to the other AI Server endpoints, e.g., you can provide a RefId and Tag to help categorize the request, and for Queue requests, you can provide a ReplyTo URL to send a POST request to when the request is complete.
Crop Image​
var response = client.PostFilesWithRequest(new CropImage {
X = 50,
Y = 50,
Width = 150,
Height = 150
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")]
);
var videoUrl = response.Outputs[0].Url;
videoUrl.DownloadFileTo(outputFileName);
Queue Crop Image​
var response = client.PostFilesWithRequest(new QueueCropImage {
X = 50,
Y = 50,
Width = 150,
Height = 150
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")]
);
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
await Task.Delay(1000);
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
}
// Download the cropped video
var videoUrl = status.Outputs[0].Url;
videoUrl.DownloadFileTo($"cropped-image-{status.RefId}.jpg");
Convert Image​
var response = client.PostFilesWithRequest(new ConvertImage {
OutputFormat = ImageOutputFormat.Gif
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")]
);
var videoUrl = response.Outputs[0].Url;
videoUrl.DownloadFileTo(outputFileName);
Queue Convert Image​
var response = client.PostFilesWithRequest(new QueueConvertImage {
OutputFormat = ImageOutputFormat.Png
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")]
);
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
await Task.Delay(1000);
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
}
// Download the converted video
var videoUrl = status.Outputs[0].Url;
videoUrl.DownloadFileTo(outputFileName);
Scale Image​
var response = client.PostFilesWithRequest(new ScaleImage {
Width = 1280,
Height = 720,
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")]
);
var videoUrl = response.Outputs[0].Url;
videoUrl.DownloadFileTo(outputFileName);
Queue Scale Image​
var response = client.PostFilesWithRequest(new QueueScaleImage {
Width = 1280,
Height = 720,
ReplyTo = "https://example.com/my/reply/endpoint" // optional
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image")]
);
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
await Task.Delay(1000);
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
}
// Download the scaled video
var videoUrl = status.Outputs[0].Url;
videoUrl.DownloadFileTo($"scaled-image-{status.RefId}.jpg");
Watermark Image​
var response = client.PostFilesWithRequest(new WatermarkImage {
Position = WatermarkPosition.BottomRight
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image"),
new UploadFile("watermark_image.png", File.OpenRead("files/watermark_image.png"), "watermark")]
);
var videoUrl = response.Outputs[0].Url;
videoUrl.DownloadFileTo(outputFileName);
Queue Watermark Image​
var response = client.PostFilesWithRequest(new QueueWatermarkImage {
Position = WatermarkPosition.BottomRight
},
[new UploadFile("test_image.jpg", File.OpenRead("files/test_image.jpg"), "image"),
new UploadFile("watermark_image.png", File.OpenRead("files/watermark_image.png"), "watermark")]
);
var status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
await Task.Delay(1000);
status = await client.GetAsync(new GetJobStatus { RefId = response.RefId });
}
// Download the watermarked video
var videoUrl = status.Outputs[0].Url;
videoUrl.DownloadFileTo($"watermarked-image-{status.RefId}.jpg");