Quickly generate drop-shadowed thumbnails from the command line using
ImageMagick's convert
, optionally specifying a maximum geometry
for either (or both) dimensions.
You can choose whether or not to draw a thin (1-pixel), grey (#777)
border around the image, or leave it off with the
-nb
/ --no-border
option.
If you want the image at its original size, just with the border and
dropshadow added, then use the -nr
/ --no-resize
option.
All supported options are documented in the script's --help
output. Long,
human-readable versions of the options are also accepted; see the source,
below.
GIST='https://gist.github.com/ernstki/83c65441303728d13642259466f4ae57'
mkdir -p ~/bin
curl -sL -o ~/bin/thumb "$GIST/raw/thumb.sh"
chmod a+x ~/bin/thumb
If you do not already have $HOME/bin
in your search path for executables,
this will safely add that, assuming you use the Bash shell:
if ! tr : \\n <<<"$PATH" | grep -q "^$HOME/bin$"; then
if [[ -f ~/.bash_profile ]]; then
profile=~/.bash_profile
else
profile=~/.profile
fi
echo '
# add $HOME/bin at the *end* of your PATH
export PATH="$PATH:$HOME/bin"' >> $profile
source $profile
unset profile
fi
$ thumb --help thumb - create thumbnails for specified image(s) usage: thumb [XXXx] [xYYY] [-g GEOM] [-s SUFF] [-nb] [-nr] [-k] [-v] img [...] thumb [-h|--help] where: XXXx max horizontal pixel size (default: 400) xYYY max vertical pixel size (default: keep aspect ratio) -g GEOM arbitrary geometry specification understood by ImageMagick (see: https://tinyurl.com/y6z8g45w#geometry) -s SUFF optional suffix for the thumnbail (default: '_thumb') -nb omit border around image (default: 1 px) -nr do not resize the image (long opt: '--no-resize') -k keep intermediate files (scaled version of ) -v be verbose; show what is being done -h you're looking at it ;-) Options that take an argument may be "cuddled"; e.g., '-g24x24'. Long options like '--geometry=GEOM' and '--keep' are also accepted. Problems? --> https://gist.github.com/ernstki/83c65441303728d13642259466f4ae57
Long options --geometry
, --suffix
, --no-border
(or --borderless
),
--no-resize
(or --original-size
), --keep
, and --verbose
are also
recognized. The ones that take arguments may either have the argument affixed
(with an =
), or separated by whitespace, like --geometry 200x200 --suffix _200px
.
Thumbnail and drop-shadow all PNG images in the current directory, using the default maximum horizontal (x dimension) size of 400 pixels:
thumb image*.png
# creates 'image1_thumb.png', 'image2_thumb.png', ...
Same as above, but limit the horizontal dimension to 200 px instead, keeping
the aspect ratio; also print what is happening with the
-v
/ --verbose
option:
thumb -v 200x image.png
# output:
# [image.png] scaling to geometry 200x
# [image.png] adding a drop shadow (bg=none, color=#777, width=1)
# [image.png] removing intermediate file 'screencap_scaled.png'.
Same as above, but specify different suffix from the default of _thumb
:
thumb -s @200x image.png 200x
# creates '[email protected]'
Note that arguments and filenames are understood in any order.
Thumbnail some JPEG images, Limiting the vertical (y) dimension to 200 pixels; good for images like panoramas that may have different x dimensions, but you want to be all the same height:
thumb pano*.jpg x200
JPEG images don't support transparency, so the drop-shadow will be drawn over a white background.
Don't draw the light grey border around the image; useful for non-rectangular images that have their own transparency, like macOS window screenshots:
# the options '--no-border' and '--borderless' are also accepted
thumb -nb window-screenshot.png
Here's a demonstration of some of the human-readable "long" options. Make a thumbnail of a photograph at exactly 0.3 megapixels, and keep the intermediate (scaled, but not drop-shadowed) image around, too:
thumb --geometry=300000@ --suffix=_3MP+shadow --keep photo.jpg
# creates 'photo_scaled.jpg' and 'photo_3MP+shadow.jpg'
See the "Image Geometry" section of the ImageMagick manual for other
examples of what you can do with the -g
/ --geometry
option.
Existing files (either thumbnails or the _scaled
intermediate files) are
overwritten silently if they exist.
If the image filenames contain any path elements (e.g.,
/path/to/image.png
), the thumbnails are output in the same directory as the
source image, rather than the current working directory. This is usually what
you want anyway, right?
Sizes specified with the XXXx
and YYYx
options are the pixel dimensions
that the image is scaled to before adding the drop shadow. If you need an
image to have exact pixel dimensions with the drop shadow, you'll need to
modify the script on your own to do that math. This was not necessary for my
simple use case of attaching screenshots to emails.
The script does not attempt to convert filetypes like JPEG that don't support transparency into another format like PNG, and that was beyond the scope of this one-hour hack; what it does instead is to force a white background.
If you're having trouble with the background color, it can be overridden by setting an environment variable, like this:
THUMB_BGCOLOR=white thumb [other options]
You can also change the width and color of the border, if you wish, using
the environment variables THUMB_BORDERWIDTH
and THUMB_BORDERCOLOR
,
respectively. Refer to the ImageMagic help for supported
color specifications.
Kevin Ernst (ernstki -at- mail.uc.edu)
- SoulSearchAndDestroy's Horizon (Chillwave - Synthwave - Retrowave Mix)
- Skitch 1.0.12 (SHA256: b2f4181f5eb40a570547054e8ec22ca9bbe490eca02a28e2389fc5d83fdc6e97); RIP
MIT.
Here's a small helper script, which will find the last screenshot you took and pass all other options through to
thumb
. Put it in~/bin/thumblast
.Modify
SCREENSHOT_PATH
for platforms other than macOS, and updateSCREENSHOT_PATTERNS
if your screenshot tool has some other file naming convention.