openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Images/Example02_SimpleImageEdit.cs
31lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Images; |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | |
| 6 | namespace OpenAI.Examples; |
| 7 | |
| 8 | public 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 | |