0

I want to get a log of each compressed image in order to see the saved size (because there are some image that after compression get bigger!) using just the terminal. Ex:

convert photo.jpg -quality 50% photo2.jpg

photo2.jpg | saved size: 100 kb

EDIT 1:

How can i implement the suggestion of @daemonburrito to the code that i was using to compress multiple images?

Here it is:

for PHOTO in /home/bnnsou/Desktop/images/*
   do
       BASE=`basename $PHOTO`
    convert "$PHOTO" -quality 40% "/home/bnnsou/Desktop/imagesCompressed/$BASE"
   done;  
K_dev
  • 249

1 Answers1

0

Yes, but some clarification of your question may be needed. It's very simple to do with a shell script; e.g., with Awk or Perl. But if your question is whether it can be done without any other tools, then I think the answer is no.

The script is very easy to write, however. You haven't listed all of your requirements, but it could be called with the same arguments as you've passed to convert, and it could simply parse the output of ls -al to get the difference between the uncompressed and compressed sizes.

You may wish to update your question by asking "How would I write a shell script to...", if I've guessed correctly about the intent of the question.

UPDATE The script:

#!/usr/bin/env bash
# script to log before-and-after sizes for imagemagick compression
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)"

Put this in a script named, for example, convert_with_logging, make it executable with chmod +x convert_with_logging, and call it with the same arguments as you called convert; i.e.,

./convert_with_logging photo.jpg -quality 50% photo2.jpg
  • I've edited the question. So based on your suggestion and on my example, where should i put the ls -al? – K_dev Aug 25 '18 at 16:50
  • Updated my original answer with a code sample. – daemonburrito Aug 25 '18 at 17:38
  • Incredible that worked. I didn't even knew that its possible to do that. But how can i do that for multiple images? I didn't understood it at the point of implement your logic in the code that i was running to compress multiple images. Please see my edit ... – K_dev Aug 25 '18 at 19:41
  • 1
    You can simply replace convert in your script with convert_with_logging.sh. However, I did notice a bug: this will only work on filenames without spaces! A little fine-tuning is in order, but if that's not a problem, it'll work. – daemonburrito Aug 26 '18 at 01:36
  • 1
    Updated the script for filenames with spaces. – daemonburrito Aug 26 '18 at 02:20
  • Please see: https://askubuntu.com/questions/1069140/how-would-i-write-a-shell-script-to-compress-multiple-images-with-imagemagick – K_dev Aug 26 '18 at 16:40
  • 1
    The answer already posted there is on the right track. Btw, I think you've got the pieces to do any permutation of the task; and, more generally, all the pieces to know how to get around in a unix shell (sh, bash, etc.), because your growth is evident. One more note, though: if you feel that you're hitting the limits of expressing your intent in Bash, feel free to use other languages like Python or even Nodejs (surprising easy to write shell scripts in JS). – daemonburrito Aug 26 '18 at 17:25