SmartAICompendium.ImageGeneration 1.1.0

dotnet add package SmartAICompendium.ImageGeneration --version 1.1.0                
NuGet\Install-Package SmartAICompendium.ImageGeneration -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SmartAICompendium.ImageGeneration" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SmartAICompendium.ImageGeneration --version 1.1.0                
#r "nuget: SmartAICompendium.ImageGeneration, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SmartAICompendium.ImageGeneration as a Cake Addin
#addin nuget:?package=SmartAICompendium.ImageGeneration&version=1.1.0

// Install SmartAICompendium.ImageGeneration as a Cake Tool
#tool nuget:?package=SmartAICompendium.ImageGeneration&version=1.1.0                

This package allow easy and typed image generation with a local Stable Diffusion model and a embedded python. Compatible with windows, linux and mac.

Generation Parameters

Name Description
textPrompt The prompt to guide the image generation.
device Device to be used for the inference.
scheduler Scheduler (sampler) to be used for diffusing.
safeMode Should NFSW be censored.
width The width in pixels of the generated image.
height The height in pixels of the generated image.
guidanceScale Guidance scale is enabled by setting it higher than 1. Higher guidance scale encourages to generate images that are closely linked to the text prompt, usually at the expense of lower image quality.
numInferenceSteps The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
seed For a choosen seed and non ancestral scheduler, a prompt will return the same result. If not specified, wil randomize.
negativeTextPrompt The prompt not to guide the image generation. Ignored when not using guidance.
imagePrompt The initial image used to guide the image generation.
strength How much to add noise to the imagePrompt. must be between 0 and 1, where 1 ignore the image. It will influence the number of steps executed.
controlNets Controls to condition the generation with different spatial contexts.
stepCallback Callback after each step, get the current step and it's image preview. Can slow down the process.

Usage examples

Image to image with ControlNet

The model can be a huggingface repoId ("{user}/{model}"), civitai modelId ("civitai/{modelId}") or local folder containing the model. The model will be downloaded if not already present (see SmartAICompendium.Common)

var testPromptImage = File.ReadAllBytes("/path/to/base/image.png");
var testMaskImage = File.ReadAllBytes("/path/to/mask.png");

var model = new ImageInference("runwayml/stable-diffusion-v1-5", BaseImageModelType.SD15);

var outputImage = model.Generate("green apple, best quality",   
    safeMode: true,
    negativeTextPrompt: "red, earth, bad quality",
    numInferenceSteps: 15,
    strength: 0.8f,
    stepCallback: new Action<StepCallbackEvent>((callbackEvent) =>
        File.WriteAllBytes($"/path/to/imagetestpreview-{callbackEvent.Step}.png", callbackEvent.Preview)),
    imagePrompt: testPromptImage, 
    controlNets: new ControlNetPrompt[] {
        new() {
            Model = ControlNetType.Canny,
            ControlImage = testPromptImage,
            ConditioningScale = 1
        },
        new() {
            Model = ControlNetType.OpenPose,
            ControlImage = testPromptImage,
            ConditioningScale = 1
        },
        new() {
            Model = ControlNetType.InPaint,
            ControlImage = testMaskImage,
            ConditioningScale = 1
        }
});

File.WriteAllBytes("/path/to/output.png", outputImage);

ControlNet preview

var testPromptImage = File.ReadAllBytes("path/to/base/image.jpg");

var outputImage = ImageInference.GenerateControlNetPreview(new()
    {
        Model = ControlNetType.LineartAnime,
        ControlImage = testPromptImage,
        ConditioningScale = 1
    });

File.WriteAllBytes("/path/to/output.png", outputImage);

Loading packages before use

if not done, the packages will be installed at the class initialization.

ImageInference.Update();

Support My work

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 149 2/1/2024
1.0.2 108 1/30/2024
1.0.1 104 1/30/2024
1.0.0 104 1/30/2024