Skip to content

Instantly share code, notes, and snippets.

@Calinou
Last active July 13, 2022 00:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Calinou/f75fd3e1731ea95c75bc7bcb555e433a to your computer and use it in GitHub Desktop.
Save Calinou/f75fd3e1731ea95c75bc7bcb555e433a to your computer and use it in GitHub Desktop.
Shell script to prepare images and upscale them using SFTGAN
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
SFTGAN_DIR="/home/hugo/Documents/Git/xinntao/SFTGAN"
print_header() {
echo -e "\n\e[1m[*] $1\e[0m"
}
print_header "Cleaning up old files... (NOTE: Make a backup of textures in samples/ as they will be removed at the end!)"
rm -rf "$SFTGAN_DIR"/data/{samples_byteimg,samples_colorimg,samples_segprob,samples_result}/*
print_header "Extracting the alpha channel into a separate texture..."
parallel convert {} -alpha extract "PNG24:{.}.alpha.png" ::: "$SFTGAN_DIR"/data/samples/*.png
# This step removes the alpha channel, so we need to extract alpha before.
# Apply a black background to avoid brightening the texture when merging it back.
print_header "Tiling textures with a 3×3 pattern..."
parallel montage {} -background black +clone +clone +clone +clone +clone +clone +clone +clone -tile 3x3 -geometry +0+0 PNG24:{} ::: "$SFTGAN_DIR"/data/samples/*.png
print_header "Cropping textures to improve upscaling performance and decrease VRAM requirements..."
parallel mogrify -gravity center -crop 37.5x37.5%+0+0 PNG24:{} ::: "$SFTGAN_DIR"/data/samples/*.png
for i in {1..2}; do
print_header "Resizing images with waifu2x (pass $i/2)..."
# Don't run this in parallel as the GPU would quickly run out of VRAM.
for image in "$SFTGAN_DIR"/data/samples/*.png; do
waifu2x -i "$image" -o "$image"
done
done
print_header "Running SFTGAN segmentation..."
(cd "$SFTGAN_DIR/pytorch_test/" && python test_segmentation.py)
print_header "Running SFTGAN upscaling..."
(cd "$SFTGAN_DIR/pytorch_test/" && python test_sftgan.py)
print_header "Cropping final images to match the originals..."
parallel mogrify -gravity center -crop 88.8888x88.8888%+0+0 {} ::: "$SFTGAN_DIR"/data/samples_result/*.png
print_header "Recomposing the alpha channel in images with transparency..."
parallel convert "{= s/.alpha.png/.png/ =}" {} -alpha off -compose copy_opacity -composite "{= s/.alpha.png/.png/ =}" ::: "$SFTGAN_DIR"/data/samples_result/*.alpha.png
print_header "Removing temporary alpha channel textures..."
rm -f "$SFTGAN_DIR"/data/samples_result/*.alpha.png
print_header "Removing original sample textures..."
rm -f "$SFTGAN_DIR"/data/samples/*.png
print_header "Optimizing the resulting images losslessly..."
parallel oxipng --strip all ::: "$SFTGAN_DIR"/data/samples_result/*.png
print_header "Done."
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
SFTGAN_DIR="/path/to/SFTGAN/repository"
print_header() {
echo -e "\n\e[1m[*] $1\e[0m"
}
print_header "Running pass 1..."
./sftgan.sh
print_header "Resizing images..."
for image in "$SFTGAN_DIR"/data/samples_result/*.png; do
mogrify -resize 50% "$image"
mv "$image" "$SFTGAN_DIR"/data/samples/
done
print_header "Running pass 2..."
./sftgan.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment