I am trying to loop a set of jpg images in a folder. The problem is some of the images have the extension in all lower case letters (.jpg) and others have all capital letters (.JPG).
I am trying to use variable modifiers to change the extensions as the loop progresses.
I figured out how to phrase the for var in LIST part but I can't get the modifiers to replace the extensions correctly.
Essentially I need the jpg files to convert to files with .mpc and .cache extensions.
I end up with files that have names like FILE.jpg.mpc
or FILE.JPG.jpg
when I need the file to be FILE.mpc
and FILE.cache
Here is my function that works great if you only use for i in *.jpg
but when you add for i in *.{jpg,JPG}
everything falls apart.
Again here is what I have so far.
imow()
{
clear
local i DIMENSIONS RANDOM
# find all jpg files and create temporary cache files from them
for i in *.{jpg,JPG}
do
# create random direcotories in case you are running this function more than once at the same time. it prevents cross-over.
RANDOM="$(mktemp --directory)"
"${RANDOM}" 2>/dev/null
echo -e "\\nCreating two temporary cache files: ${RANDOM}/${i%%.jpg,JPG}.mpc + ${RANDOM}/${i%%.{jpg,JPG}}.cache\\n"
DIMENSIONS="$(identify -format '%wx%h' "${i}")"
convert "${i}" -monitor -filter 'Triangle' -define filter:support='2' -thumbnail "${DIMENSIONS}" -strip \
-unsharp '0.25x0.08+8.3+0.045' -dither None -posterize '136' -quality '82' -define jpeg:fancy-upsampling='off' \
-define png:compression-filter='5' -define png:compression-level='9' -define png:compression-strategy='1' \
-define png:exclude-chunk='all' -auto-level -enhance -interlace 'none' -colorspace 'sRGB' "${RANDOM}/${i%%.{jpg,JPG}}.mpc"
clear
for i in "${RANDOM}"/*.mpc
do
if [ -f "${i}" ]; then
echo -e "\\nOverwriting original file with optimized self: ${i} >> ${i%%.mpc}.jpg\\n"
convert "${i}" -monitor "${i%%.mpc}.jpg"
if [ -f "${i%%.mpc}.jpg" ]; then
mv "${i%%.mpc}.jpg" "${PWD}"
rm -fr "${RANDOM}"
clear
else
clear
echo 'Error: Unable to find the optimized image and therefore can'\''t overwrite the original.'
echo
exit 1
fi
fi
done
done
}
Like I mentioned if you run it with just the .jpg extension it works great. here is a working example.
imow()
{
clear
local i DIMENSIONS RANDOM
# find all jpg files and create temporary cache files from them
for i in *.jpg
do
# create random directories in case you are running this function more than once at the same time. it prevents cross-over.
RANDOM="$(mktemp --directory)"
"${RANDOM}" 2>/dev/null
echo -e "\\nCreating two temporary cache files: ${RANDOM}/${i%%.jpg}.mpc + ${RANDOM}/${i%%.jpg}.cache\\n"
DIMENSIONS="$(identify -format '%wx%h' "${i}")"
convert "${i}" -monitor -filter 'Triangle' -define filter:support='2' -thumbnail "${DIMENSIONS}" -strip \
-unsharp '0.25x0.08+8.3+0.045' -dither None -posterize '136' -quality '82' -define jpeg:fancy-upsampling='off' \
-define png:compression-filter='5' -define png:compression-level='9' -define png:compression-strategy='1' \
-define png:exclude-chunk='all' -auto-level -enhance -interlace 'none' -colorspace 'sRGB' "${RANDOM}/${i%%.jpg}.mpc"
clear
for i in "${RANDOM}"/*.mpc
do
if [ -f "${i}" ]; then
echo -e "\\nOverwriting original file with optimized self: ${i} >> ${i%%.mpc}.jpg\\n"
convert "${i}" -monitor "${i%%.mpc}.jpg"
if [ -f "${i%%.mpc}.jpg" ]; then
mv "${i%%.mpc}.jpg" "${PWD}"
rm -fr "${RANDOM}"
clear
else
clear
echo 'Error: Unable to find the optimized image and therefore can'\''t overwrite the original.'
echo
exit 1
fi
fi
done
done