For the projects that I work on, be it the computer stuff or my cars, I like to document it with taking photos of the process and the result. Sometimes it is easier to use a photo to show something, instead of many lines of code.
Some of the photos is shared on online communities like forums, and I don’t like posts where the images are so big that I have to side-scroll (even on my big monitors.. ), so I adjust the size of the images that I post myself. I also self-host the images, so it also helps me minimize the used space on the VM and also helps in the bandwitdh use on the website.
It started with an easy little bash script, that loops through all the files it sees in the directory from where the script is run and for each .jpg file, it resize it to correct aspect ratio of the original and set the corresponding size correctly (e.g. 4032×3024 -> 1024×768). The 768^ sets the lowest size of the image to 768, meaning if the image is 800×1024 the 800 will be set to 768 and the 1024 will follow the ratio, and if it is 1024×800, it will still use the 800 to change the size of the image.
for FILE in *.jpg; do
echo "Converting: $FILE"
magick $FILE -resize 768^ \
converted/cnv-$FILE
done
After the file is resized it is output’ed to the directory converted and the letters cnv- is added to the file, making it easy for me to see that it is converted. It will most likely be changed later on, but I still want something to differentiate it from the old file, in case they are copied to the same directory at one point.
The new script is quite bigger, because it checks a lot of things before it even attempts to resize the image. It has checks to see if the variable actually holds a directory name instead of a file name, and it checks if the image has already been converted.
It ensures that the file trying to be resized is an image, and also strips the metadata from the converted image, making sure it keeps the default orientation. I think a script that rotates the image for you, in the case it is needed is better than not keeping the original orientation of the image.
So, here’s the new script
#!/usr/bin/env bash
# Check to make sure that the directory is not empty.
shopt -s nullglob
# Create directory if it does not exist
mkdir -p converted
# Initialize counters
count_new=0
count_skipped=0
# Check if the file is an image.
checkImage() {
local filename="$1"
# Check if it is a file we are handling
if [[ -f "$filename" ]]; then
# Read out the mime-type of the file
MIME_TYPE=$(file --mime-type -b "$filename")
# Check if it is an image, then resize it.
if [[ "$MIME_TYPE" == image/* ]]; then
resizeImg "$filename"
((count_new++))
return 0
fi
fi
}
# Resize the image
resizeImg() {
local filename="$1"
# Resize the image, and strip out metadata
magick "$filename" -auto-orient -strip -resize 768^ "converted/cnv-$filename"
}
# Loop through and check if the file is an image
for FILE in *; do
# Check if the variabel is actually an directory
# We check for a file in checkImage, but its good to stop as early
# as possible.
if [[ -d "$FILE" ]]; then
continue
fi
# Check if the file has been converted earlier
if [[ -f "converted/cnv-$FILE" ]]; then
((count_skipped++))
continue
fi
checkImage "$FILE"
done
# Output the amount of images that have been resized
echo "---------------------------------------"
echo "Done! Prosessed in $(pwd):"
echo " - New images converted: $count_new"
echo " - Previously converted: $count_skipped"
echo "---------------------------------------"
If I put it in one of my paths in the shell, I would be able to use this in any picture folder that I have, and make sure that every image is uniform in size, and also make sure that the it does not matter if it is a .jpg, .jpeg, .png etc.
Leave a Reply
You must be logged in to post a comment.