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
convert
in your script withconvert_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:36sh
,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