Edit an image - JZO001/Forge.OpenAI GitHub Wiki
Manipulate images with DALL·E models
The image edits endpoint allows you to edit and extend an image by uploading a mask. The transparent areas of the mask indicate where the image should be edited, and the prompt should describe the full new image, not just the erased area.
static async Task Main(string[] args)
{
// This example demonstrates, how you can ask OpenAI to edit an existing image you provide.
// More information: https://platform.openai.com/docs/guides/images/edits
//
// The very first step to create an account at OpenAI: https://platform.openai.com/
// Using the loggedIn account, navigate to https://platform.openai.com/account/api-keys
// Here you can create apiKey(s)
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((builder, services) =>
{
services.AddForgeOpenAI(options => {
options.AuthenticationInfo = builder.Configuration["OpenAI:ApiKey"]!;
});
})
.Build();
IOpenAIService openAi = host.Services.GetService<IOpenAIService>()!;
// Images should be in png format with ARGB
ImageEditRequest request = new ImageEditRequest();
request.Image = new BinaryContentData()
{
ContentName = "Original Image",
SourceStream = File.OpenRead("image_edit_original.png")
};
using (request.Image.SourceStream)
{
request.Mask = new BinaryContentData()
{
ContentName = "Mask Image",
SourceStream = File.OpenRead("image_edit_mask.png")
};
using (request.Mask.SourceStream)
{
request.Prompt = "A boy cycling away on a bicycle on the road";
HttpOperationResult<ImageEditResponse> response =
await openAi.ImageService
.EditImageAsync(request, CancellationToken.None)
.ConfigureAwait(false);
if (response.IsSuccess)
{
Console.WriteLine(response.Result!);
response.Result!.ImageData.ForEach(imageData => OpenUrl(imageData.ImageUrl));
}
else
{
Console.WriteLine(response);
}
}
}
}