0

I have an image resizer(bottom). Now I want to use it to batch resize pngs. Problem is to run it only once for each file. This snippet creates new images over the created ones:

for image in find . -type f -name "*.png"; do
   /Users/dev/scripts/./imre.sh "$image" 200 133
done

Resizer:

#!/bin/bash

###### LOCAL VARIABLES
convert=/usr/bin/convert
identify=/usr/bin/identify
image=$1
heigth=$2
width=$3
usage_message="Usage: imre image heigth width"
usage_example="Example: imre anyImage.png 50 50"
space=""

###### ACTIONS
clear
echo $space
echo "*** START image resizer script ***"
echo $space

if [ -z "$image" ] || [ -z "$heigth" ] || [ -z "$width" ] ; 
    then
    echo $usage_message
    echo $space
    echo $usage_example
    echo $space
    echo "*** END image resizer script ***" 
    echo $space ; exit 1
fi

echo "To modify $image to $heigth x $width (HxW)"
echo "Given image data: "
identify $image
convert $image -resize "${heigth}"x"${width}" $image
echo "$1"

echo $space
echo "*** END image resizer script ***" 
echo $space
exit 0
3
  • convert "$image" -resize ... - missing $. Also best if you will always quote variables. here are some cases when it isn't needed but in general it is much better... Commented Mar 18, 2017 at 19:01
  • Thanks for comment. Sorry I was editing it. The imre does run. But the first snippet runs in circles. Has to do it only once. Commented Mar 18, 2017 at 19:25
  • I am using @setempler first solution. Works like charm. Thanks to all. Commented Mar 18, 2017 at 20:08

2 Answers 2

2

Your for loop expands not to the expected, because find is not called.

Instead, every single word of the find command (including the arguments) is used as argument for the for loop.

A solution is to use find in a sub-shell, as in

for image in `find . -type f -name "*.png"`; do
   /Users/dev/scripts/./imre.sh "$image" 200 133
done

or

for image in $(find . -type f -name "*.png"); do
   /Users/dev/scripts/./imre.sh "$image" 200 133
done

You can easily check it with an echo:

for image in `find . -type f -name "*.png"`; do
   echo "/Users/dev/scripts/./imre.sh \"$image\" 200 133"
done

For your example, the result of the check:

for image in find . -type f -name "*.png"; do
   echo "/Users/dev/scripts/./imre.sh \"$image\" 200 133"
done

yields:

/Users/dev/scripts/./imre.sh "find" 200 133
/Users/dev/scripts/./imre.sh "." 200 133
/Users/dev/scripts/./imre.sh "-type" 200 133
/Users/dev/scripts/./imre.sh "f" 200 133
/Users/dev/scripts/./imre.sh "-name" 200 133
/Users/dev/scripts/./imre.sh "*.png" 200 133
Sign up to request clarification or add additional context in comments.

Comments

0

Just to simplify for others to read. This is the full batcher working. Uses imre resizer:

#!/bin/bash

###### LOCAL VARIABLES
heigth=$1
width=$2
usage_message="Usage: batch_imre heigth width"
usage_example="Example: batch_imre 50 50"
space=""

clear
echo $space
echo "*** START batch image resizer script ***"
echo $space

if [ -z "$heigth" ] || [ -z "$width" ] ; 
    then
    echo $usage_message
    echo $space
    echo $usage_example
    echo $space
    echo "*** END batch image resizer script ***" 
    echo $space ; exit 1
fi

for image in `find . -type f -name "*.png"`; do
    /Users/dev/scripts/./imre.sh "$image" $heigth $width
done

echo $space
echo "*** END batch image resizer script ***" 
echo $space
exit 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.