Skip to content

Instantly share code, notes, and snippets.

@hagure
Last active January 2, 2023 23:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagure/b787a5dfbc630ade55c9a9c64b9955bd to your computer and use it in GitHub Desktop.
Save hagure/b787a5dfbc630ade55c9a9c64b9955bd to your computer and use it in GitHub Desktop.
[Trim Images with ImageMagick] Trim extra space around images with imagemagick. Takes in an image.ext as input, & outputs image-trimmed.ext as output. See example here: https://twitter.com/hagure/status/1610056235826696192 #imagemagick #bash #shell
#!/bin/bash
# REQUIRES [IMAGEMAGICK](http://imagemagick.org)
# brew install imagemagick
# Get the path to the file
thefilepath=$(dirname "$1")
# Get the file's name
thefilename=$(basename "$1")
# Get the file's base name (may fail for images with multiple extensions)
thefilebasename=$(echo $thefilename | rev | cut -f 2- -d '.' | rev)
# Get the file's extension
thefileext=${thefilename##*.}
# higher fuzz = more cropping
convert "$1" -fuzz 10% -trim +repage "$thefilepath/$thefilebasename-trimmed.$thefileext"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment