igolaizola / bulkai

AI image generation in bulk automatically
MIT License
195 stars 41 forks source link

(feature request) Save Prompt Text in Image Metadata #54

Open socraticbc opened 1 year ago

socraticbc commented 1 year ago

Parameters for saving the full prompt text in the image metadata (and/or filename length) and PNG compression would be incredibly appreciated. Thank you, I use this every day.

igolaizola commented 8 months ago

This task can be accomplished using ExifTool. However, I haven't found an elegant method to do this from Go, and I prefer not to embed an external binary with bulkai.

LouisDeconinck commented 8 months ago

This would actually be very helpful. I wrote a custom script in Python to do this using the Pyexiv2 library, but would be a lot cleaner if this were done by bulkai.

Perhaps you can include it as some kind of optional add-on that has an external library?

Unfortunately, I don't know any Go, but I found this package and article that might be helpful:

I also tried my hand at some ChatGPT consultations, which came up with this code. Although I do think that it is recommended to use a specific library to handle exif data.

package main

import (
    "fmt"
    "os"
)

func main() {
    imageFilename := "example.jpg"

    // Open the image file for reading and writing
    file, err := os.OpenFile(imageFilename, os.O_RDWR, 0644)
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file.Close()

    // Define the Exif metadata header for the title
    exifTitle := "My title"

    // Seek to the beginning of the file
    _, err = file.Seek(0, 0)
    if err != nil {
        fmt.Println("Error seeking to the beginning of the file:", err)
        return
    }

    // Write the Exif metadata title to the file
    _, err = file.Write([]byte("\xFF\xE1" + "\x00\x16" + "Exif" + "\x00\x00"))
    if err != nil {
        fmt.Println("Error writing Exif header:", err)
        return
    }

    // Write the title length and title itself to the file
    _, err = file.Write([]byte(fmt.Sprintf("%c%s", len(exifTitle)+2, exifTitle)))
    if err != nil {
        fmt.Println("Error writing title to the file:", err)
        return
    }

    fmt.Println("Title successfully written to image metadata.")
}