This bash script searches for image files in a specified directory, enhances and converts them to JPEG format, and renames them by removing "-final" extensions and appending ".jpg" extensions. The script uses commands like find
, convert
, and mv
to perform these operations, relying on ImageMagick and parameter expansion for file manipulation.
find /Users/briancullinan/.q3a/unpacked/baseq3vm -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 "$i" +clone -enhance -despeckle -alpha on -channel alpha -evaluate multiply 0.25 -composite -quality 50% "$o" || true; \
rm "$i"; \
mv "$o" "${i%.*}.jpg"; \
else \
mv "$o" "${i%.*}.jpg"; \
fi; \
done;
#!/bin/bash
IMAGE_FORMATS=(jpeg jpg heic tga png svg webp wal)
# Define the directory to search for images
IMAGE_DIR="/Users/briancullinan/.q3a/unpacked/baseq3vm"
# Loop through each image format and find matching files
while IFS= read -r image; do
# Extract the file name without extension
filename="${image%.*}"
# Determine the output file name
output_filename="${filename%-final*}-final.jpg"
# Check if the output file already exists
if [! -f "$output_filename" ]; then
# Enhance the image using ImageMagick
convert "$image" +clone -enhance -despeckle -alpha on -channel alpha -evaluate multiply 0.25 -composite -quality 50% "$output_filename" || true
# Delete the original image
rm "$image"
# Rename the output file to the original name with a.jpg extension
mv "$output_filename" "${image%.*}.jpg"
else
# If the output file already exists, rename it to the original name with a.jpg extension
mv "$output_filename" "${image%.*}.jpg"
fi
done < <(find "$IMAGE_DIR" -iname "${IMAGE_FORMATS[*]/%/.}" | sort)
Code Breakdown
The code is a bash script designed to:
find
command is used to search for files in the specified directory /Users/briancullinan/.q3a/unpacked/baseq3vm
.find
command's output is piped to a while
loop using the read
command.${i%.*}
and store it in variable o
.o
.[! -f "$o" ]
. If it does not exist:
convert
command from ImageMagick to enhance and convert the image to JPEG format.find
commandImageMagick
package with convert
command|| true
part in the convert
command is used to ignore any errors that may occur during the conversion process.rm
command is used to delete the original file after conversion.mv
command is used to move the converted file to the original file's directory with a ".jpg" extension.