openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Images/Example03_SimpleImageVariationAsync.cs
30lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Images; |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace OpenAI.Examples; |
| 8 | |
| 9 | public partial class ImageExamples |
| 10 | { |
| 11 | [Test] |
| 12 | public async Task Example03_SimpleImageVariationAsync() |
| 13 | { |
| 14 | ImageClient client = new("dall-e-2", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 15 | |
| 16 | string imageFilePath = Path.Combine("Assets", "images_dog_and_cat.png"); |
| 17 | |
| 18 | ImageVariationOptions options = new() |
| 19 | { |
| 20 | Size = GeneratedImageSize.W256xH256, |
| 21 | ResponseFormat = GeneratedImageFormat.Bytes |
| 22 | }; |
| 23 | |
| 24 | GeneratedImage variation = await client.GenerateImageVariationAsync(imageFilePath, options); |
| 25 | BinaryData bytes = variation.ImageBytes; |
| 26 | |
| 27 | using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png"); |
| 28 | bytes.ToStream().CopyTo(stream); |
| 29 | } |
| 30 | } |
| 31 | |