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