Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit d311605

Browse files
committed
Fix issue with compressed gifs
1 parent d5ef111 commit d311605

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

OnnxStack.Console/Examples/StableDiffusionGif.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using SixLabors.ImageSharp;
88
using SixLabors.ImageSharp.Formats.Gif;
99
using SixLabors.ImageSharp.PixelFormats;
10+
using SixLabors.ImageSharp.Processing;
1011
using System.Diagnostics;
1112

1213
namespace OnnxStack.Console.Runner
@@ -60,37 +61,45 @@ public async Task RunAsync()
6061
schedulerOptions.Width = model.SampleSize;
6162
schedulerOptions.Height = model.SampleSize;
6263

64+
var repeatCount = 0;
65+
var frameDelay = 0;
6366

6467
using Image<Rgba32> gifDestination = new(schedulerOptions.Width, schedulerOptions.Height);
6568
{
6669
var gifMetaData = gifDestination.Metadata.GetGifMetadata();
67-
gifMetaData.RepeatCount = 0; // Loop
70+
gifMetaData.RepeatCount = (ushort)repeatCount; // Loop
6871

6972
using (var gifSource = await Image.LoadAsync(Path.Combine(_outputDirectory, "Source.gif")))
73+
using (var frame = gifSource.Frames.CloneFrame(0))
7074
{
7175
for (int i = 0; i < gifSource.Frames.Count; i++)
7276
{
73-
// Get Frame as Image
74-
var frame = gifSource.Frames.CloneFrame(i).CloneAs<Rgba32>();
77+
// Draw each frame on top of the last to fix issues with compressed gifs
78+
frame.Mutate(x => x.DrawImage(gifSource.Frames.CloneFrame(i), 1f));
7579

7680
// Save Debug Output
7781
await frame.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Frame.png"));
7882

7983
// Set prompt Image, Run Diffusion
80-
promptOptions.InputImage = new InputImage(frame);
84+
promptOptions.InputImage = new InputImage(frame.CloneAs<Rgba32>());
8185
var result = await _stableDiffusionService.GenerateAsImageAsync(model, promptOptions, schedulerOptions);
8286

8387
// Save Debug Output
8488
await result.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Output.png"));
8589

90+
// Set Fraem metadata
91+
var metadata = result.Frames.RootFrame.Metadata.GetGifMetadata();
92+
metadata.FrameDelay = frameDelay;
93+
8694
// Add Result to Gif
8795
gifDestination.Frames.InsertFrame(i, result.Frames.RootFrame);
8896
OutputHelpers.WriteConsole($"Frame: {i + 1}/{gifSource.Frames.Count}", ConsoleColor.Cyan);
8997
}
90-
91-
// Save Result
92-
await gifDestination.SaveAsGifAsync(Path.Combine(_outputDirectory, $"Result.gif"));
9398
}
99+
100+
// Save Result
101+
await gifDestination.SaveAsGifAsync(Path.Combine(_outputDirectory, $"Result.gif"));
102+
94103
}
95104
}
96105

0 commit comments

Comments
 (0)