image magik commands | Cell 1 | Cell 3 | Search

The code is a pair of scripts, one for Windows and one for Unix/Linux, that use image conversion tools to process multiple image file types in bulk. The scripts convert images to various formats, resize and scale them, and apply enhancements to create final images with a consistent ".jpg" format.

Cell 2

#windows
# for /r %%v in (*.pcx or *.jpg or *.jpeg) do convert -colorspace RGB "%%v" "%%~dpv%%~nv.tga" ; done
#

#/bin/bash
find /Users/briancullinan/jupyter_ops/.output -iname '*.jpeg' -o -iname '*.jpg' -o -iname '*.heic' -o -iname '*.tga' -o -iname '*.png' -o -iname '*.svg' -o -name '*.webp' -o -name '*.wal' | \
while IFS= read -r i; do \
o="${i%.*}"; \
o="${o%-final*}-final.jpg"; \
if [ ! -f "$o" ]; \
then convert -density 600 "$i" -resize "600^<" -scale 50% "$o" || true && \
    convert "$o" +clone -enhance -despeckle -alpha on -channel alpha -evaluate multiply 0.25 -composite -quality 50% "$o" || true; \
fi; \
done;

echo "done"

What the code could have been:

#!/bin/bash

# Define the input directory and the output directory
INPUT_DIR="/Users/briancullinan/jupyter_ops/.output"
OUTPUT_DIR="${INPUT_DIR}/converted"

# Create the output directory if it does not exist
mkdir -p "${OUTPUT_DIR}"

# Define the file extensions to be processed
FILE_EXTENSIONS=(jpeg jpg heic tga png svg webp wal)

# Use find to locate all files in the input directory with the specified extensions
for i in $(find "${INPUT_DIR}" -type f -name "*.*" -print0 | xargs -0 -I {} basename {}); do
    # Extract the filename and extension from the file path
    filename="${i%.*}"
    extension="${i##*.}"

    # Check if the file extension is in the list of supported extensions
    if [[ " ${FILE_EXTENSIONS[@]} " =~ " ${extension} " ]]; then
        # Construct the output filename
        output_filename="${filename%-final*}-final.jpg"

        # Construct the output file path
        output_file_path="${OUTPUT_DIR}/${output_filename}"

        # Check if the output file already exists
        if [! -f "${output_file_path}" ]; then
            # Use ImageMagick to convert the file to RGB and resize it
            convert -density 600 "${i}" -resize "600^<" -scale 50% "${output_file_path}" || true

            # Use ImageMagick to apply image enhancement and cleanup
            convert "${output_file_path}" +clone -enhance -despeckle -alpha on -channel alpha -evaluate multiply 0.25 -composite -quality 50% "${output_file_path}" || true
        fi
    fi
done

echo "done"

Breakdown of the Code

Section 1: Windows Batch Script

# Windows
# for /r %%v in (*.pcx or *.jpg or *.jpeg) do convert -colorspace RGB "%%v" "%%~dpv%%~nv.tga" ; done

Section 2: Unix/Linux Bash Script

#/bin/bash
find /Users/briancullinan/jupyter_ops/.output -iname '*.jpeg' -o -iname '*.jpg' -o -iname '*.heic' -o -iname '*.tga' -o -iname '*.png' -o -iname '*.svg' -o -name '*.webp' -o -name '*.wal' | \
while IFS= read -r i; do \
o="${i%.*}"; \
o="${o%-final*}-final.jpg"; \
if [! -f "$o" ]; \
then convert -density 600 "$i" -resize "600^<" -scale 50% "$o" || true && \
    convert "$o" +clone -enhance -despeckle -alpha on -channel alpha -evaluate multiply 0.25 -composite -quality 50% "$o" || true; \
fi; \
done;

echo "done"