0

I tried the bitmap class and the information stored in PropertyItems doesn't appear to have any stable diffusion prompt information (eve if the Id isn't 0x0010). Any help would be appreciated.

 using (var image = new Bitmap(imagePath))
            {
                // Get the metadata property items
                var propertyItems = image.PropertyItems;

                // Find the Stable Diffusion prompt property
                var stableDiffusionPrompt = Array.Find(propertyItems, p => p.Id == 0x0110); // 0x0110 is the ID for the Stable Diffusion prompt property
                if (stableDiffusionPrompt != null)
                {
                    string prompt = GetStringFromProperty(stableDiffusionPrompt);
                    Debug.WriteLine($"Stable Diffusion Prompt: {prompt}");
                }
                else
                {
                    Debug.WriteLine("Stable Diffusion Prompt not found in the image metadata.");
                }
            }
Philip
  • 147
  • 7
  • After some research it turns out PNG files store this text as plain ASCII at the start of the file. Now I just need to be able to read this text, modify it, and store it back as a PNG file. If anyone knows how to do this, I would be appreciative. – Philip Jul 14 '23 at 16:19

1 Answers1

0

After much research, it turns out that each chunk in a PNG has its own checksum and even though the text is stored at the start of the file in plain text, it isn't as simple as modifying that text. However, the SixLabors library has an easy way to handle this use case. See code below ...

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualBasic.Devices;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
using SixLabors.ImageSharp.Metadata.Profiles.Iptc;
using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
using static System.Net.Mime.MediaTypeNames;


namespace Test_Libpng
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string imagePath = "C:\\Test1.png";
            string imageSavePath = "C:\\TestSave1.png";

            using (var image = SixLabors.ImageSharp.Image.Load(imagePath))
            {

                // Get the PNG metadata
                var pngMetadata = image.Metadata.GetFormatMetadata(PngFormat.Instance);

                SixLabors.ImageSharp.Formats.Png.PngTextData SaveData = new SixLabors.ImageSharp.Formats.Png.PngTextData();

                //assumes each image has one and only one text chunk which is true for all images from StableDiffustion
                try
                {
                    SaveData = pngMetadata.TextData[0];
                }
                catch (Exception)
                {

                }
                finally {

                    string keyword = "keywords here";
                    string text = "text here";

                    // Create a new text chunk
                    var newTextChunk = new PngTextData(keyword, text, "lang", "translate");
                    pngMetadata.TextData.Remove(SaveData);
                    pngMetadata.TextData = new PngTextData[] { newTextChunk };

                    // Save the modified image with updated metadata
                    image.Save(imageSavePath);
                }
            }
        }
    }
}
Philip
  • 147
  • 7