openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joseharriaga/stable-api-listing

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/Images/Example02_SimpleImageEditAsync.cs

32lines · modecode

1using NUnit.Framework;
2using OpenAI.Images;
3using System;
4using System.IO;
5using System.Threading.Tasks;
6
7namespace OpenAI.Examples;
8
9public partial class ImageExamples
10{
11 [Test]
12 public async Task Example02_SimpleImageEditAsync()
13 {
14 ImageClient client = new("dall-e-2", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
15
16 string imageFilePath = Path.Combine("Assets", "images_flower_vase.png");
17 string prompt = "A vase full of beautiful flowers.";
18 string maskFilePath = Path.Combine("Assets", "images_flower_vase_with_mask.png");
19
20 ImageEditOptions options = new()
21 {
22 Size = GeneratedImageSize.W512xH512,
23 ResponseFormat = GeneratedImageFormat.Bytes
24 };
25
26 GeneratedImage edit = await client.GenerateImageEditAsync(imageFilePath, prompt, maskFilePath, options);
27 BinaryData bytes = edit.ImageBytes;
28
29 using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png");
30 await bytes.ToStream().CopyToAsync(stream);
31 }
32}
33