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
  • I don't think you have carefully thought out your strategy as I see several question all created by you on the same subject here and here! – George Udosen Aug 26 '18 at 16:52
  • @GeorgeUdosen They are all different: one is for resizing and other is for compressing one image. I've created this one to not confuse the other topics as each question achieve a different result. – K_dev Aug 26 '18 at 17:02
  • If those questions solved your problems then why haven't you accept the answers given? – George Udosen Aug 26 '18 at 17:05
  • @GeorgeUdosen i only forgot to accept the answer in this question: https://askubuntu.com/questions/1068850/resize-multiple-images-with-imagemagick-from-a-folder-to-other-and-keep-the-nam/1068861?noredirect=1#comment1752921_1068861 . The other i had accepted – K_dev Aug 26 '18 at 17:21
  • I have accepted your answer for that one. Thanks for your time @GeorgeUdosen – K_dev Aug 26 '18 at 17:23

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нιη
  • 35,660
  • It works but its replacing the image extension by ".cnvrt" ... how to keep the same extension from the original image? – K_dev Aug 26 '18 at 17:36
  • 1
    emm, I wrote like that to don't overwrite existing files if you put source and destination same, but you can change all ${OUTPUT_FILENAME%.*}.cnvrt to ${OUTPUT_FILENAME} as your need – αғsнιη Aug 26 '18 at 17:38