Skip to content

Instantly share code, notes, and snippets.

@glegoux
Last active August 30, 2017 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glegoux/eb57c141a73dc58845bf5b00d0a48418 to your computer and use it in GitHub Desktop.
Save glegoux/eb57c141a73dc58845bf5b00d0a48418 to your computer and use it in GitHub Desktop.
[Bash] Reduce images to send a batch of images by email for example
#!/usr/bin/env bash
#
# compress-img.bash
#
# Reduce size by 50% for each given image by copying that in
# a new folder ./compressed.
#
# You can reduce size or quality of each image, if you don't want,
# send a link to application where there are your images. And this
# script is usless for your use case.
#
# It is very handly to send reduced images by email.
# Because often the sum of disk space for all attachments
# to send is limited, don't be exceed 25MB for Gmail.
# (see policies of your email service).
#
# You can archive the new set of images before
# sending (.zip, .tar.gz ...) to reduce attachment size again.
#
# Reference:
# https://support.google.com/mail/answer/6584?co=GENIE.Platform%3DDesktop&hl=en
# helper
height() {
local img="$1"
identify -ping -format '%h' "${img}"
}
width() {
local img="$1"
identify -ping -format '%w' "${img}"
}
size() {
local img="$1"
ls -lh "${img}" | awk '{print $5}'
}
# script
if [[ "${FUNCNAME[0]}" == "main" ]]; then
cd `dirname "$0"`
mkdir -p ./compressed/
for img in $@; do
convert "${img}" -resize 50% "./compressed/${img}" && \
echo -n "./compressed/${img} created ... " && \
size "./compressed/${img}"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment