santa hat
deerdeer nosedeer glow
Sign In

Automating Prompt Metadata Injection from PNGs to JPGs

6

pngPrompt2jpg is a Bash script that extracts metadata from PNG files and injects it into corresponding JPG files. It utilizes the ExifTool command-line tool for metadata extraction and injection. It is useful when you like to post-process images and want to inject the prompt info back into the image after your software deleted them.

Prerequisites

- This guide is aimed at Linux/Mac users for now. I will make a Windows version later, but it's basically the same thing with a different syntax.

- ExifTool must be installed on your system. You can install it by following the installation instructions provided on the ExifTool website.


To install it on Debian/Ubuntu-based systems, use:

$ sudo apt-get install libimage-exiftool-perl

Copy the script

We will create a script that automates the extraction and injection of metadata. Here’s the script that does this:

#!/bin/bash

CONFIG_FILE="$HOME/.pngPrompt2jpg_config"

# Function to show usage
usage() {
    echo "Usage: pngPrompt2jpg [directory] [--overwrite] [--save-default]"
    echo "Options:"
    echo "  -h                Show this help message"
    echo "  --overwrite       Overwrite existing JPG files"
    echo "  --save-default    Save the provided directory and options as the default"
    echo "Example:"
    echo "  pngPrompt2jpg /path/to/directory --overwrite --save-default"
}


# Check if the user asked for help
if [ "$1" == "-h" ]; then
    usage
    exit 0
fi

# Check if the user wants to overwrite existing JPG files
if [ "$2" == "--overwrite" ]; then
    OVERWRITE="-overwrite_original"
fi

# If a directory is provided as an argument, use it
if [ "$#" -ge 1 ]; then
    dir="$1"
else
    # If there's a default directory saved, use it
    if [ -f "$CONFIG_FILE" ]; then
        dir=$(head -n 1 "$CONFIG_FILE")
        # Check if the overwrite option is saved
        if [ -f "$CONFIG_FILE" ] && grep -q "OVERWRITE" "$CONFIG_FILE"; then
            source "$CONFIG_FILE"
        fi
    else
        dir="."
    fi
fi

# Check if the user wants to save the directory and options as default
if [ "$3" == "--save-default" ]; then
    echo "dir=\"$dir\"" > "$CONFIG_FILE"
    echo "OVERWRITE=\"$OVERWRITE\"" >> "$CONFIG_FILE"
fi

# Loop through each png file in the directory
for png_file in "${dir}"/*.png; do
    # Get the base name without extension
    base_name=$(basename "${png_file}" .png)
    # Corresponding jpg file
    jpg_file="${dir}/${base_name}.jpg"
    
    # Extract metadata from the png file
    metadata=$(exiftool -Parameters -b "${png_file}")
    
    # Check if metadata is not empty
    if [ ! -z "$metadata" ]; then
        # Inject metadata to the jpg file
        exiftool ${OVERWRITE} -UserComment="${metadata}" "${jpg_file}"
        echo "Metadata injected to ${jpg_file}"
    else
        echo "No metadata found in ${png_file}"
    fi
done


Make the script globally accessible

Save the script in a file called pngPrompt2jpg in your home directory. Now, move this file to /usr/local/bin and make it executable. This will allow you to run this script from any directory.

$ sudo mv ~/pngPrompt2jpg /usr/local/bin/pngPrompt2jpg
$ sudo chmod +x /usr/local/bin/pngPrompt2jpg


Usage

⚠️ Important: the PNG and JPG file names must be identical. It will automatically copy PNG info to JPG info based on the filename. So take care not to misname files for instance

$ pngPrompt2jpg [directory] [--overwrite] [--save-default]

- [directory] (optional): The directory containing PNG and JPG files. If not provided, the script will default to the current directory.

- --overwrite (optional): Overwrite existing JPG files with the injected metadata. If this option is not specified, the script will create a JPG backup for each modified JPG.

- --save-default (optional): Save the provided directory and options as the default for future invocations.

Examples

  1. Inject metadata from PNG files in the current directory into corresponding JPG files:

$ pngPrompt2jpg
  1. Inject metadata from PNG files in a specific directory and overwrite existing JPG files:

$ pngPrompt2jpg /path/to/directory --overwrite
  1. Inject metadata from PNG files in a specific directory, overwrite existing JPG files, and save the directory and options as default:

$ pngPrompt2jpg /path/to/directory --overwrite --save-default

Configuration

The script allows you to save a default directory and options for future invocations. The configuration is stored in the $HOME/.pngPrompt2jpg_config file.

By default, the script will use the last saved directory and options as the default. To change the default directory or options, you can modify the configuration file manually or use the --save-default option to save the current directory and options as the default.

Troubleshooting

If you struggle with something, please notify me in the comments.

License

This project is licensed under the MIT License (link).

6