0

Currently im using this command in terminal to compress a single image with imagemagick:

./convert_with_logging photo.jpg -quality 50% photo2.jpg

convert_with_logging is a script that contains:

INPUT_FILENAME="$1"
OUTPUT_FILENAME="$4"

ORIGINAL_SIZE=$(wc -c "${INPUT_FILENAME}" | cut -d ' ' -f1)
convert "$@"
COMPRESSED_SIZE=$(wc -c "${OUTPUT_FILENAME}" | cut -d ' ' -f1)

echo "${OUTPUT_FILENAME} | saved size: $(expr $ORIGINAL_SIZE - $COMPRESSED_SIZE)"

Note: this script converts and it also log the compressed size (ex: imageA.jpg | saved size: 1994825 )

Now currently im using this command to compress multiple images (that are jpg and jpeg):

for PHOTO in /home/dvs/Desktop/proj1/src/images/*.{jpeg,jpg}
   do
       BASE=`basename $PHOTO`
    ./convert_with_logging "$PHOTO" -quality 40% "/home/dvs/Desktop/proj1/src/compressed/$BASE"
   done; 

Now how can i convert all this last command in order to type "./convert_multi_with_logging" and get the same result?

I think that we need to add something like this to the script:

inpath="/home/dvs/Desktop/proj1/src/images/"

outpath="/home/dvs/Desktop/proj1/src/compressed/"
K_dev
  • 249

1 Answers1

4

Change your script to something like this:

for PHOTO in "$1"/*.{jpeg,jpg};
do

    OUTPUT_FILENAME="$(basename "$PHOTO")"
    ORIGINAL_SIZE=$(wc -c "${PHOTO}" | cut -d ' ' -f1)

    convert "$PHOTO" $2 $3 "$4/${OUTPUT_FILENAME%.*}.cnvrt"
    COMPRESSED_SIZE=$(wc -c "$4/${OUTPUT_FILENAME%.*}.cnvrt" | cut -d ' ' -f1)
    echo "${OUTPUT_FILENAME%.*}.cnvrt | saved size: $(expr $ORIGINAL_SIZE - $COMPRESSED_SIZE)"

done

and execute it with these arguments.

./convert_multi_with_logging /path/to/source -quality 50% /path/to/destination
αғsнιη
  • 36,350