openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Images/Example03_SimpleImageVariation.cs
29lines · 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 Example03_SimpleImageVariation() |
| 12 | { |
| 13 | ImageClient client = new("dall-e-2", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 14 | |
| 15 | string imageFilePath = Path.Combine("Assets", "images_dog_and_cat.png"); |
| 16 | |
| 17 | ImageVariationOptions options = new() |
| 18 | { |
| 19 | Size = GeneratedImageSize.W256xH256, |
| 20 | ResponseFormat = GeneratedImageFormat.Bytes |
| 21 | }; |
| 22 | |
| 23 | GeneratedImage variation = client.GenerateImageVariation(imageFilePath, options); |
| 24 | BinaryData bytes = variation.ImageBytes; |
| 25 | |
| 26 | using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png"); |
| 27 | bytes.ToStream().CopyTo(stream); |
| 28 | } |
| 29 | } |
| 30 | |