openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-1135

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Images/Example02_SimpleImageEdit.cs

31lines · modecode

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