Created
May 7, 2024 11:55
-
-
Save danieloneill/30a2bfb6295a93b6a943597584e8b9c4 to your computer and use it in GitHub Desktop.
Bash script to generate and display images from a Stable Diffusion server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# It's expected that you'll have the following commands/programs in your $PATH: | |
# - base64 | |
# - basename | |
# - cat | |
# - cp | |
# - echo | |
# - mktemp | |
# - printf | |
# | |
# ... very likely you do, BUT make sure you ALSO have: | |
# - curl | |
# - jq | |
# | |
# because you need those too. | |
# | |
# === Update these variables to suit your system === | |
# | |
SDURL=http://192.168.1.10:7860 | |
# If called with -s (and nothing else), your last generated image will be saved to wherever you specift here: | |
DEFAULTSAVEDIR=$HOME/Pictures | |
# | |
# === SD Parameters | |
# | |
PROMPT="Photo of a mountainous British Columbian landscape, UFO sighting, Sasquatch sighting" | |
NEGPROMPT="Vancouver, city, snow" | |
SAMPLER="DPM++ 2M SDE Karras" | |
STEPS=30 | |
CFGSCALE="7.5" | |
RESTORE_FACES="false" | |
# | |
# === Other small tweaks === | |
# | |
# Path to a "state" file where we "remember" the most recently generated image. This is used when the script is called with -s to save it somewhere. | |
LASTIMAGE=/tmp/genimg.last | |
# | |
# ==== The rest is script ==== | |
# | |
function genImage { | |
# The raw JSON response is slurped into $FEH. | |
FEH=$(curl -s --json @- $SDURL/sdapi/v1/txt2img << EOF | |
{ | |
"prompt": "${PROMPT}", | |
"negative_prompt": "${NEGPROMPT}", | |
"styles": [ | |
], | |
"sampler_name": "${SAMPLER}", | |
"steps": ${STEPS}, | |
"cfg_scale": ${CFGSCALE}, | |
"width": 512, | |
"height": 512, | |
"restore_faces": ${RESTORE_FACES} | |
} | |
EOF | |
) | |
# This grabs the resulting image data (Base64 encoded) into $FOO | |
FOO=$(echo $FEH | jq -r '.images[0]') | |
# This is some Kitty (I think?) voodoo. Header, raw image Base64, terminator: | |
printf "\033[0C\033]1337;File=inline=1:" | |
echo -n $FOO | |
printf "\007\n" | |
# Next we save a (decoded) copy to a temp file and remember where it was: | |
BLEH=$(mktemp --suffix .png) | |
echo $BLEH > $LASTIMAGE | |
echo $FOO | base64 -d > $BLEH | |
} | |
function saveLast { | |
DEST=$1 | |
SRC=$(cat $LASTIMAGE) | |
echo "Saving $SRC to $DEST" | |
cp $SRC $DEST | |
} | |
WS=0 | |
while getopts 'lcp:n:s:S:C:rR' OPTION; do | |
case "$OPTION" in | |
l) | |
cat $LASTIMAGE | |
exit 0 | |
;; | |
c) | |
WS=1 | |
;; | |
p) | |
PROMPT="$OPTARG" | |
;; | |
n) | |
NEGPROMPT="$OPTARG" | |
;; | |
s) | |
STEPS="$OPTARG" | |
;; | |
S) | |
SAMPLER="$OPTARG" | |
;; | |
C) | |
CFGSCALE="$OPTARG" | |
;; | |
r) | |
RESTORE_FACES="false" | |
;; | |
R) | |
RESTORE_FACES="true" | |
;; | |
?) | |
cat <<EOF | |
Usage $(basename $0) [-l] [-c [dest]] [-p <prompt>] [-n <nprompt>] [-s <steps>] | |
[-S <sampler>] [-C <cfg scale>] [-r] [-R] | |
-l Print the file location of the last generated image | |
-c [dest] Copy the last generated image to [dest] or "${DEFAULTSAVEDIR}" if unspecified | |
-p <prompt> Specify the positive prompt string | |
-n <nprompt> Specify the negative prompt string | |
-s <steps> Specify the samping steps (eg: "25") | |
-S <sampler> Specify the sampler by name (eg: "Euler a") | |
-C <cfgscale> Specift the CFG scale (eg: "7.5") | |
-r Disable face restoration | |
-R Enable face restoration | |
EOF | |
>&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $(($OPTIND -1)) | |
if [ 1 -eq $WS ]; then | |
if [ 0 -lt $# ]; then | |
saveLast $1 | |
else | |
saveLast $DEFAULTSAVEDIR | |
fi | |
exit 0 | |
fi | |
genImage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script requires a few utilities to be installed, and expects your terminal to support Kitty graphics (I think). Konsole from any recent Plasma release works alright, so I suspect most moderns GUI terminal emulators probably will too.