Files
Martin Rizzo 9f311abffb chore: Update thumbnail generation script for improved quality
This commit modifies the thumbnail generation script at "/styles/make.sh"
to adjust the final JPEG quality of thumbnails to 80%, enhancing clarity
while maintaining file size efficiency.

Additionally, the script now handles non-square base images by resizing
the square ones to 384x384 pixels. Previously, if any image was not square,
the entire process would be aborted with no thumbnails generated; now, the
script proceeds with processing square images even if some are non-square.
2026-02-19 20:07:22 -03:00

275 lines
9.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# File : styles/make.sh
# Purpose : Extract workflows and thumbnails based on images generated by ComfyUI
# Author : Martin Rizzo | <martinrizzo@gmail.com>
# Date : Feb 19, 2026
# Repo : https://github.com/martin-rizzo/ComfyUI-ZImagePowerNodes
# License : MIT
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ComfyUI-ZImagePowerNodes
# ComfyUI nodes designed specifically for the "Z-Image" model.
#
# Copyright (c) 2026 Martin Rizzo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}" .sh) # script name without extension
SCRIPT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")") # script directory
THUMBNAILS_DIR="${SCRIPT_DIR}/samples"
WORKFLOWS_DIR="${SCRIPT_DIR}/samples_wf"
SOURCE_DIR="${SCRIPT_DIR}/src"
VERSION="0.1.0"
HELP="
Usage: ./$SCRIPT_NAME.sh [COMMAND] [OPTIONS]
Extract workflows and thumbnails based on the images generated by ComfyUI.
The thumbnails generated by this script are used in the style gallery.
Available commands:
workflows , wf Extract workflows from the images.
thumbnails, thumb Generate 384x384 thumbnails for the style gallery.
all (default) Extract workflows and generate thumbnails.
Available options:
-n, --no-color Disable color messages.
-h, --help Show this help message and exit.
Examples:
$SCRIPT_NAME.sh
$SCRIPT_NAME.sh workflows --no-color
"
# ANSI escape codes for colored terminal output
RED='\e[91m'; YELLOW='\e[93m'; GREEN='\e[92m'; CYAN='\e[96m'; RESET='\e[0m'
disable_color() { RED=''; YELLOW=''; GREEN=''; CYAN=''; RESET=''; }
#============================= ERROR MESSAGES ==============================#
# Display help message
help() { echo "${HELP}"; }
# Display a message in cyan color
info() { echo -e "${CYAN}[${GREEN}INFO${CYAN}]${RESET} $1"; }
# Display a warning message
warning() {
local oneline=false;
while [[ $# -gt 0 ]]; do
if [[ "$1" == "-1" ]]; then oneline=true; shift
else break;
fi
done
[[ "$oneline" == false ]] && echo
echo -e "${CYAN}[${YELLOW}WARNING${CYAN}]${RESET} $1" >&2;
[[ "$oneline" == false ]] && echo
}
# Display an error message
error() { echo -e "\n${CYAN}[${RED}ERROR${CYAN}]${RESET} $1" >&2; }
# Displays a fatal error message and exits the script
fatal_error() {
error "$1"; shift
while [[ $# -gt 0 ]]; do
echo -e " ${CYAN}\xF0\x9F\x9B\x88 $1${RESET}" >&2
shift
done
echo; exit 1
}
#================================= HELPERS =================================#
print_relpath() {
local file="$1" base="$2" format="$3"
local relpath
[[ -z "$base" ]] && base="$SCRIPT_DIR"
relpath="${file##"$base"}"
[[ $relpath == '/'* ]] && relpath="${relpath:1}"
[[ -n "$format" ]] && relpath="${format//@relpath/$relpath}"
echo -e "${relpath}"
}
#================================ COMMANDS =================================#
# Performs initial checks and creates necessary directories if they don't exist.
#
initial_check() {
if [[ ! -e "$THUMBNAILS_DIR" ]]; then
info "Creating thumbnails folder..."
mkdir -p "${THUMBNAILS_DIR}"
fi
if [[ ! -e "$WORKFLOWS_DIR" ]]; then
info "Creating workflows folder..."
mkdir -p "${WORKFLOWS_DIR}"
fi
if [[ ! -e "$SOURCE_DIR" ]]; then
fatal_error "Source directory not found." \
"Create a 'src' directory and include all images you wish to process inside it. Then, rerun the script."
fi
}
# Builds thumbnail images from the PNG files in the WORKFLOWS directory.
#
build_thumbnails() {
# find all PNGs in the WORKFLOWS directory
local images
readarray -t images < <(find "${WORKFLOWS_DIR}" -type f -name '*.png')
# if no images are found, show message and exit
if [[ ${#images[@]} -eq 0 ]]; then
info "No images found in ${WORKFLOWS_DIR}, nothing to do."
return 1
fi
# group the images into squared and rectangular categories
local squared_images=()
local rectangular_images=()
info "Analyzing images... (this might take a while)"
for image in "${images[@]}"; do
read -r width height <<< "$(identify -format "%w %h" "$image")"
local diff=$(( width - height ))
if (( ${diff#-} <= 2 )); then squared_images+=("$image")
else rectangular_images+=("$image")
fi
done
# report non-square images and exit if any are found
if [[ ${#rectangular_images[@]} -gt 0 ]]; then
warning -1 "The following images are not squared:"
for image in "${rectangular_images[@]}"; do
print_relpath "$image" "${WORKFLOWS_DIR}" " - @relpath"
done
fi
# build thumbnails of squared images
if [[ ${#squared_images[@]} -gt 0 ]]; then
info "Building ${#squared_images[@]} thumbnails... (this might take a while)"
mogrify -path "${THUMBNAILS_DIR}" -format jpg -resize 384x384 -quality 80% "${squared_images[@]}"
info "Done."
fi
}
# Extracts workflows from each PNG file in the SOURCE directory.
#
extract_workflows() {
echo "Extracting workflows..."
# find all PNGs in the SOURCE directory
local images
readarray -t images < <(find "${SOURCE_DIR}" -type f -name '*.png')
# if no images are found, show message and exit
if [[ ${#images[@]} -eq 0 ]]; then
info "No images found in ${SOURCE_DIR}, nothing to do."
return 1
fi
# process each image individually
for image in "${images[@]}"; do
local image_name workflow
image_name=$(basename "$image" .png)
workflow="${WORKFLOWS_DIR}/${image_name}.json"
workflow_image="${WORKFLOWS_DIR}/${image_name}.png"
# each extracted workflow is accompanied by the image that generated it.
# If this companion image already exists in the WORKFLOWS directory,
# it means the extraction has been done and should be skipped.
if [[ -e "${workflow_image}" ]]; then
warning -1 "Skipping workflow \"$(print_relpath "${workflow}")\""
continue
fi
info "Generating workflow \"$(print_relpath "${workflow}")\""
exiftool -b -Workflow "${image}" > "${workflow}"
cp "${image}" "${workflow_image}"
done
}
# #===========================================================================#
# #////////////////////////////////// MAIN ///////////////////////////////////#
# #===========================================================================#
main() {
local no_color=false # no color mode flag
local show_help=false # show help flag
local show_version=false # show version flag
local command="all" # default command to execute
while [[ $# -gt 0 ]]; do
if [[ $1 == -* ]]; then
case $1 in
-n|--nc|--no-color) no_color=true ;;
-h|--help) show_help=true ;;
-v|--version) show_version=true ;;
*)
fatal_error "Invalid option: \"$1\"" "Use --help for usage." ;;
esac
else
command=$1;
fi
shift
done
# disable color output if --no-color option is provided
if [[ $no_color == true ]]; then disable_color; fi
# display help message and exit if --help option is provided
if [[ $show_help == true ]]; then help; exit 0; fi
# display command version and exit
if [[ $show_version == true ]]; then echo "$VERSION"; exit 0; fi
# execute the specified command
case $command in
workflows|wf)
initial_check
extract_workflows
exit 0
;;
thumbnails|thumb)
initial_check
build_thumbnails
exit 0
;;
all)
initial_check
extract_workflows
build_thumbnails
exit 0
;;
*) fatal_error "Invalid command: \"$command\"" \
"Use --help for usage."
esac
}
main "$@"