Image Transform Endpoints

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

using var fsImage = File.OpenRead("files/test_image.jpg");
var response = client.PostFileWithRequest(new CropImage {
        X = 50,
        Y = 50,
        Width = 150,
        Height = 150
    },
    new UploadFile("test_image.jpg", fsImage, "image"));

var videoUrl = response.Results[0].Url;
videoUrl.DownloadFileTo(saveToPath);

Queue Crop Image

using var fsImage = File.OpenRead("files/test_image.jpg");
var response = client.PostFileWithRequest(new QueueCropImage {
        X = 50,
        Y = 50,
        Width = 150,
        Height = 150
    },
    new UploadFile("test_image.jpg", fsImage, "image"));

GetArtifactGenerationStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
    status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId });
    await Task.Delay(1000);
}

// Download the cropped image
File.WriteAllBytes(saveToPath, status.Results[0].Url.GetBytesFromUrl());

Convert Image

using var fsImage = File.OpenRead("files/test_image.jpg");
var response = client.PostFileWithRequest(new ConvertImage {
        OutputFormat = ImageOutputFormat.Gif
    },
    new UploadFile("test_image.jpg", fsImage, "image"));

File.WriteAllBytes(saveToPath, response.Results[0].Url.GetBytesFromUrl());

Queue Convert Image

using var fsImage = File.OpenRead("files/test_image.jpg");
var response = client.PostFileWithRequest(new QueueConvertImage {
        OutputFormat = ImageOutputFormat.Png
    },
    new UploadFile("test_image.jpg", fsImage, "image"));

GetArtifactGenerationStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
    status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId });
    await Task.Delay(1000);
}

// Download the converted image
File.WriteAllBytes(saveToPath, status.Results[0].Url.GetBytesFromUrl());

Scale Image

using var fsImage = File.OpenRead("files/test_image.jpg");
var response = client.PostFileWithRequest(new ScaleImage {
        Width = 1280,
        Height = 720,
    },
    new UploadFile("test_image.jpg", fsImage, "image"));

File.WriteAllBytes(saveToPath, response.Results[0].Url.GetBytesFromUrl());

Queue Scale Image

using var fsImage = File.OpenRead("files/test_image.jpg");
var response = client.PostFileWithRequest(new QueueScaleImage {
        Width = 1280,
        Height = 720,
        ReplyTo = "https://example.com/my/reply/endpoint" // optional
    },
    new UploadFile("test_image.jpg", fsImage, "image"));

GetArtifactGenerationStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
    status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId });
    await Task.Delay(1000);
}

// Download the scaled image
File.WriteAllBytes(saveToPath, status.Results[0].Url.GetBytesFromUrl());

Watermark Image

using var fsImage = File.OpenRead("files/test_image.jpg");
using var fsWatermark = File.OpenRead("files/watermark_image.png");
var response = client.PostFilesWithRequest(new WatermarkImage {
        Position = WatermarkPosition.BottomRight
    }, [
        new UploadFile("test_image.jpg", fsImage, "image"),
        new UploadFile("watermark_image.png", fsWatermark, "watermark")
    ]);

File.WriteAllBytes(saveToPath, response.Results[0].Url.GetBytesFromUrl());

Queue Watermark Image

using var fsImage = File.OpenRead("files/test_image.jpg");
using var fsWatermark = File.OpenRead("files/watermark_image.png");
var response = client.PostFilesWithRequest(new QueueWatermarkImage {
        Position = WatermarkPosition.BottomRight
    }, [
        new UploadFile("test_image.jpg", fsImage, "image"),
        new UploadFile("watermark_image.png", fsWatermark, "watermark")
    ]);

GetArtifactGenerationStatusResponse status = new();
while (status.JobState is BackgroundJobState.Started or BackgroundJobState.Queued)
{
    status = await client.GetAsync(new GetArtifactGenerationStatus { RefId = response.RefId });
    await Task.Delay(1000);
}

// Download the watermarked image
File.WriteAllBytes(saveToPath, status.Results[0].Url.GetBytesFromUrl());