Bash: Batch Resize Images - bash

I'm working on a Bash script that will take an array of directories, iterate through it, create a directory named "processed" inside each directory, and run a command on each file within the directory. Here's my code (read the comment in the code to see what I'm stuck on). Any ideas?
#!/bin/bash
command -v convert >/dev/null 2>&1 || {
echo >&2 "Convert is not installed. Aborting.";
exit 1;
}
declare -a directories_to_process=(
"$HOME/Desktop/Album 1"
"$HOME/Desktop/Album 2"
"$HOME/Desktop/Album 3"
);
for directory in "${directories_to_process[#]}"
do
if [ -d "$directory" ]; then
if [ ! -d "$directory/processed" ]; then
mkdir "$directory/processed"
fi
# Insert code to run the following command on each file in $directory:
#
# convert $directory/$filename -resize 108x108^ -gravity center -extent 108x108 $directory/processed/$filename
fi
done
UPDATE:
Here is the working script:
#!/bin/bash
command -v convert >/dev/null 2>&1 || {
echo >&2 "Convert is not installed. Aborting.";
exit 1;
}
directories_to_process=(
"$HOME/Desktop/Album 1"
"$HOME/Desktop/Album 2"
"$HOME/Desktop/Album 3"
);
for directory in "${directories_to_process[#]}"
do
[[ -d $directory ]] || continue
mkdir -p "$directory/processed"
for infile in "$directory"/*.jpg
do
outfile="$directory/processed/${infile##*/}"
convert "$infile" \
-resize '108x108^' \
-gravity center \
-extent 108x108 \
"$outfile"
done
done

Add this in the commented-out area:
for infile in "$directory"/*; do
outfile="$directory/processed/${infile##*/}"
convert "$infile" \
-resize '108x108^' \
-gravity center \
-extent 108x108 \
"$outfile"
done
A few other notes:
Instead of nesting a great deal of logic inside of if [ -d "$directory" ], consider putting [[ -d $directory ]] || continue at the top of the loop to reduce the nesting depth. (Unlike [ ], quoting is not needed inside [[ ]] in this case).
Instead of testing [ ! -d "$directory/processed" ] and using that to decide whether to create the directory, consider unconditionally running mkdir -p "$directory/processed", which will simply exit with a successful status if the directory already exists.
Consider replacing command -v convert with type convert, which is somewhat better known than the command -v syntax but will have the same effect.
You don't need the declare -a when declaring an array variable outside of a function; simply directories_to_process=( ... ) will work.

Related

Can't pass validation to update my app icon with imagemagick and ghoscript in xcode

I am trying to update the icon of my app to be able to differentiate in which environment it is, I have been guided by this tutorial and these othe post with similar problems working with ImageMagick and Ghostscript, however my problem is in the validation that I have in my script, but xcode they indicate that I have to install imagemagick and ghostscript, however I verify that I have it correctly installed, any ideas on how to fix it?
Error caught in xcode in report navigator
Console of my computer with the components correctly installed
I attach the script that I am using
# 942v's Script
#!/bin/sh
export PATH=/opt/local/bin/:/opt/local/sbin:$PATH:/usr/local/bin:
convertPath=`which convert`
gsPath=`which gs`
if [[ ! -f ${convertPath} || -z ${convertPath} ]]; then
convertValidation=true;
else
convertValidation=false;
fi
if [[ ! -f ${gsPath} || -z ${gsPath} ]]; then
gsValidation=true;
else
gsValidation=false;
fi
if [[ "$convertValidation" = true || "$gsValidation" = true ]]; then
echo "WARNING: Skipping Icon versioning, you need to install ImageMagick and ghostscript (fonts) first, you can use brew to simplify process:"
if [[ "$convertValidation" = true ]]; then
echo "brew install imagemagick"
fi
if [[ "$gsValidation" = true ]]; then
echo "brew install ghostscript"
fi
exit 0;
fi
buildPlist=$INFOPLIST_FILE
version="$MARKETING_VERSION"
build_num="$CURRENT_PROJECT_VERSION"
IMAGES_RIBBON="${SRCROOT}/CompileImages/${CONFIGURATION}.png"
caption="$build_num"
echo $caption
function abspath() { pushd . > /dev/null; if [ -d "$1" ]; then cd "$1"; dirs -l +0; else cd "`dirname \"$1\"`"; cur_dir=`dirs -l +0`; if [ "$cur_dir" == "/" ]; then echo "$cur_dir`basename \"$1\"`"; else echo "$cur_dir/`basename \"$1\"`"; fi; fi; popd > /dev/null; }
function processIcon() {
base_path=$1
echo "base_path: $base_path"
#this is the change
target_path=$base_path
width=`identify -format %w ${base_path}`
height=`identify -format %h ${base_path}`
band_height=$((($height * 20) / 100))
band_position=$(($height - $band_height))
text_position=$(($band_position - 3))
point_size=$(((13 * $width) / 100))
echo "Path: $IMAGES_RIBBON"
echo "Image dimensions ($width x $height) - band height $band_height # $band_position - point size $point_size"
BASE_TMP_PATH="/tmp"
#
# blur band and text
#
convert $IMAGES_RIBBON -resize ${width}x${height} $BASE_TMP_PATH/ribbon.png
convert ${base_path} -blur 10x8 $BASE_TMP_PATH/blurred.png
convert $BASE_TMP_PATH/blurred.png -gamma 0 -fill white -draw "rectangle 0,$band_position,$width,$height" $BASE_TMP_PATH/mask.png
convert -size ${width}x${band_height} xc:none -fill 'rgba(0,0,0,0.2)' -draw "rectangle 0,0,$width,$band_height" $BASE_TMP_PATH/labels-base.png
convert -background none -size ${width}x${band_height} -pointsize $point_size -fill white -gravity center -gravity South caption:"$caption" $BASE_TMP_PATH/labels.png
convert ${base_path} $BASE_TMP_PATH/blurred.png $BASE_TMP_PATH/mask.png -composite $BASE_TMP_PATH/temp.png
rm $BASE_TMP_PATH/blurred.png
rm $BASE_TMP_PATH/mask.png
#
# compose final image
#
filename=New${base_file}
convert $BASE_TMP_PATH/temp.png $BASE_TMP_PATH/labels-base.png -geometry +0+$band_position -composite $BASE_TMP_PATH/labels.png -geometry +0+$text_position -geometry +${w}-${h} -composite $BASE_TMP_PATH/ribbon.png -composite "${target_path}"
# clean up
rm $BASE_TMP_PATH/temp.png
rm $BASE_TMP_PATH/labels-base.png
rm $BASE_TMP_PATH/labels.png
rm $BASE_TMP_PATH/ribbon.png
echo "Overlayed ${target_path}"
}
appiconset=$(find ${SRCROOT}/ -name AppIcon.appiconset)
echo "appiconset: $appiconset"
if [ $CONFIGURATION = "Prod" ]; then
find "$appiconset/icons/" -name '*.png' -exec cp '{}' "$appiconset/" \;
echo "Exit"
exit 0
fi
if [ -d "$appiconset/icons/" ]
then
echo "Directory exists."
# get original icon to copy to assets
find "$appiconset/icons/" -name '*.png' -exec cp '{}' "$appiconset/" \;
else
# copy orgin to AppIcon
rsync -rv --include '*.png' --exclude '*' "$appiconset/" "$appiconset/icons/"
fi
for entry in "$appiconset"/*.png
do
processIcon $entry
done
Based on the comments, you state that running which convert from the command line gives the result /opt/homebrew/bin/convert.
Your script sets PATH to things other than /opt/homebrew/bin.
Update your script so PATH is set as follows:
export PATH=/opt/homebrew/bin:/opt/local/bin:/opt/local/sbin:$PATH:/usr/local/bin:

Unable to execute command inside double square brackets inside my Makefile

clean:
#for container_name in ${NEW_DJANGO_IMAGE_NAME} \
${NEW_MSQL_IMAGE_NAME} \
${NEW_NGINX_IMAGE_NAME} \
${NEW_REDIS_IMAGE_NAME}; \
do if [[ 'a' == 'a' ]]; then echo 'fdfdf'; fi; done;
If I do something like this it works. Now instead of this silly line
do if [[ 'a' == 'a' ]]; then echo 'fdfdf'; fi; done;
I want to write the following:
do if [[ docker ps --filter "name=^/$$container_name$$" --format '{{.Names}}' == $$container_name ]]; then echo 'fdfdf'; fi; done;
The idea is that I iterate over a number of docker containers and if it happens that some of them are running I want to stop them. So in the place of echo 'fdfdf' I want to see this line: docker container stop <CONTAINER_NAME>;
Looks as simple as hell but I can't get it to work in the Makefile...What am I doing wrong?
You apparently think that [[ cmd == "string" ]] executes cmd before performing the test. This is not the case. Use:
[[ `cmd` == "string" ]]
instead. In your case it would look like this:
do if [[ `docker ps --filter "name=^/$$container_name$$" --format '{{.Names}}'` == $$container_name ]]; then docker container stop $$container_name; fi; done;
Or, a bit more readable, maybe:
IMAGES := $(NEW_DJANGO_IMAGE_NAME) $(NEW_MSQL_IMAGE_NAME) \
$(NEW_NGINX_IMAGE_NAME) $(NEW_REDIS_IMAGE_NAME)
clean:
#for cn in $(IMAGES); do \
tmp=`docker ps --filter "name=^/$$cn$$" --format '{{.Names}}'`; \
if [ -n "$$tmp" ]; then \
docker container stop $$cn; \
fi; \
done
Note that, in this last version, we use the bourne shell test commmand ([) instead of the bash-only conditional expression ([[...]]).

sh conditionally pass an option to command

I want to do something like:
#!/bin/sh
[ -f "/tmp/nodes" ]
[[ $? -eq 0 ]] && VAL=$? ||
geth --datadir /root/.ethereum \
${VAL+"--nodekey \"/root/nodekey.txt\""} \
--networkid 1999 \
--rpc \
--rpcaddr "0.0.0.0" \
I want the option --nodekey "/root/nodekey.txt" to be passed if the file /tmp/nodes exists. How can that be done more elegantly than an if with two nearly identical commands?
--EDIT--
This is the best I've been able to get working so far:
if [ $VAL -eq 0 ]; then
/geth --datadir /root/.ethereum \
--nodekey "/root/nodekey.txt" \
# No dice
# Would be nice if this worked so I didn't need the if
# ${VAL+ --nodekey "/root/nodekey.txt" } \
--networkid 1999 \
--rpc \
--rpcaddr "0.0.0.0"
else
/geth --datadir /root/.ethereum \
--networkid 1999 \
--rpc \
--rpcaddr "0.0.0.0" \
fi
This is another line in the file and works fine:
ENODE_URL=$(/geth --datadir /root/.ethereum ${VAL+ --nodekey "/root/nodekey.txt"} --exec "${JS}" console 2>/dev/null | sed -e 's/^"\(.*\)"$/\1/')
There's a bashism here, but it's [[ $? -eq 0 ]], as [[ is a ksh extension adopted by bash. There's no point to using $? at all here, since you can just directly perform your assignment based on whether the test -f succeeds:
touch /tmp/nodes # set us up for the truthy path
if test -f /tmp/nodes; then tmp_nodes_exists=1; else unset tmp_nodes_exists; fi
printf '%s\n' /tmp/nodes ${tmp_nodes_exists+"REALLY EXISTS" "(yes, really)"}
...properly emits as output (as run with dash, perhaps the most common minimal /bin/sh interpreter):
/tmp/nodes
REALLY EXISTS
(yes, really)
By contrast, to demonstrate that the other path fails as it should:
rm -f -- /tmp/nodes # set us up for the falsey path
if test -f /tmp/nodes; then tmp_nodes_exists=1; else unset tmp_nodes_exists; fi
printf '%s\n' /tmp/nodes ${tmp_nodes_exists+"REALLY EXISTS" "(yes, really)"}
emits as output only:
/tmp/nodes

BASH dirname basename problems with spaces

Writing a script to optimize my images for the web. Having issues with filenames and directories with spaces in the names.
Heres what I have:
read -p "Enter full path from root (/) to your site... example /var/www/public_html: " path1
echo ""
#read -p "Enter in ImageMagick quality (default is 80) if unsure enter 80: " optjpg
#echo ""
#id="$(id -u optiimage)"
cmd="id -u optiimage"
eval $cmd
id=$(eval $cmd)
tmp1="${path1}/shell/optiimage/imagemagick"
tmp2="${path1}/shell/optiimage/imagemagick/jpg"
restore1="${path1}/shell/optiimage/restore"
restore2="${path1}/shell/optiimage/restore/imagemagick/jpg"
backup1="${path1}/shell/optiimage/backup"
backup2="${path1}/shell/optiimage/backup/imagemagick/jpg"
log1="${path1}/shell/optiimage/log/imagemagick/"
DATE="$(date +%a-%b-%y-%T)"
# Need user input for www path from root
##
## Make directories
##
############################################################################################################
mkdir -p ${tmp1}
mkdir -p ${tmp2}
mkdir -p ${restore1}
mkdir -p ${restore2}
mkdir -p ${backup1}
mkdir -p ${backup2}
mkdir -p ${log1}
mkdir -p ${path1}/build
echo "Processing JPG Files"
find $path1 -iname "*jpg" | \
#write out script to put on cron for image optimization
while read file;
do
# If not equal to optimage uid
# to check username id -u optimage
if [ -u "${id}" ]; then
filebase=`basename "$file" .jpg`
dirbase=`dirname "$file"`
echo "${dirbase}/${filebase}.jpg already optimized" >> ${log1}_optimized_$DATE.log
else
#simple log for size of image before optimization
ls -s $file >> ${log1}_before_$DATE.log
#Do the following if *.jpg found
filebase=`basename $file .jpg`
dirbase=`dirname $file`
echo "cp -p ${dirbase}/${filebase}.jpg ${tmp2}" >> ${path1}/build/backup_jpg.txt
echo "chown optiimage:www-data ${filebase}.jpg" >> ${path1}/build/restore_jpg.txt #${restore1}/imagemagick.sh
echo "cp -p ${filebase}.jpg ${dirbase}/${filebase}.jpg" >> ${path1}/build/restore_jpg.txt #${restore1}/imagemagick.sh
##
## ImageMagick
## Original Command:
## convert $file -quality 80 ${filebase}.new.jpg
##########################
echo "convert ${dirbase}/${filebase}.jpg -quality 80 ${tmp2}/${filebase}.jpg" >> ${path1}/build/imagemagick.txt
echo "mogrify -strip ${tmp2}/${filebase}.jpg" >> ${path1}/build/imagemagick.txt
echo "chown optiimage:www-data ${tmp2}/${filebase}.jpg" >> ${path1}/build/owner_jpg.txt
echo "rm ${dirbase}/${filebase}.jpg" >> ${path1}/build/remove_jpg.txt
echo "cp -p ${tmp2}/${filebase}.jpg ${dirbase}/" >> ${path1}/build/migrate_jpg.txt
simple log for size of image after optimization
ls -s $file >> ${log1}_after_$DATE.log
fi
done
I have edited this with suggestions some have given me. It didn't seem to work.
This works fine if I remove directories with spaces in the names otherwise it ends the name at the space and get errors directory doesn't exist.
You need to double-quote variable substitutions. This applies inside command substitutions as well as in the top-level lexical context. The only exception to this is assignment of a string variable from another string variable, e.g. str2=$str1;, although other types of variable assignments generally need quoting, such as assigning a string variable from an array slice, even if it only slices one element, e.g. str="${#:1:1}";.
Although unlikely to be a problem here, the read builtin strips leading and trailing whitespace if you provide one or more NAMEs; you can solve that by not providing any NAMEs at all, and just letting it store the whole line in the $REPLY variable by default.
You should always use the -r option of the read builtin, as that prevents its ill-advised default behavior of doing backslash interpolation/removal on the input data.
If you don't need any kind of interpolation in a string literal, prefer the '...' syntax to "...", as the former does not do any interpolation.
Prefer the [[ ... ]] expression evaluation form to the old-style [ ... ] form, as the former syntax is slightly more powerful.
Prefer the $(...) command substitution form to the old-style `...` form, as the former syntax has more favorable nesting properties (namely, no need to escape the nested command substitution delimiters).
find "$path1" -iname '*jpeg'| \
# write out script to put on cron for image optimization
while read -r; do
file=$REPLY;
# If not equal to optimage uid
# to check username id -u optimage
if [[ -u "$id" ]]; then
filebase=$(basename "$file" .jpeg);
dirbase=$(dirname "$file");
#MYBASENAME=$(basename "$1")
echo "${dirbase}/${filebase}.jpeg already optimized" >>"${log1}_optimized_$DATE.log";
fi;
done;
;
Quote your $file variable in every place where is used:
find $path1 -iname "*jpeg" | \
while read file;
do
if [ -u "${id}" ]; then
filebase=`basename "$file" .jpeg`
dirbase=`dirname "$file"`
fi
done

How to split the file path to extract the various subfolders into variables? (Ubuntu Bash)

I need help with Ubuntu Precise bash script.
I have several tiff files in various folders
masterFOlder--masterSub1 --masterSub1-1 --file1.tif
|--masterSub1-2 --masterSub1-2-1 --file2.tif
|
|--masterSub2 --masterSub1-2 .....
I need to run an Imagemagick command and save them to new folder "converted" while retaining the sub folder tree i.e. the new tree will be
converted --masterSub1 --masterSub1-1 --file1.png
|--masterSub1-2 --masterSub1-2-1 --file2.png
|
|--masterSub2 --masterSub1-2 .....
How do i split the filepath into folders, replace the first folder (masterFOlder to converted) and recreate a new file path?
Thanks to everyone reading this.
This script should work.
#!/bin/bash
shopt -s extglob && [[ $# -eq 2 && -n $1 && -n $2 ]] || exit
MASTERFOLDER=${1%%+(/)}/
CONVERTFOLDER=$2
OFFSET=${#MASTERFOLDER}
while read -r FILE; do
CPATH=${FILE:OFFSET}
CPATH=${CONVERTFOLDER}/${CPATH%.???}.png
CDIR=${CPATH%/*}
echo "Converting $FILE to $CPATH."
[[ -d $CDIR ]] || mkdir -p "$CDIR" && echo convert "$FILE" "$CPATH" || echo "Conversion failed."
done < <(exec find "${MASTERFOLDER}" -mindepth 1 -type f -iname '*.tif')
Just replace echo convert "$FILE" "$CPATH" with the actual command you use and run bash script.sh masterfolder convertedfolder

Resources