Skip to content

Instantly share code, notes, and snippets.

@luigimannoni
Created March 31, 2020 15:17
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 luigimannoni/bb76046fa5dde7cf80508db2529b8f9e to your computer and use it in GitHub Desktop.
Save luigimannoni/bb76046fa5dde7cf80508db2529b8f9e to your computer and use it in GitHub Desktop.
Imagemagick Mogrify/Resize image to nearest power of two
#!/bin/bash
# Or add to ~/bash_aliases
magickpow2 () {
echo Running imagemagick nearest pow with extension "$1"
for file in $(find . -name "$1")
do echo processing "$file"
local powwidth=$(identify -format "%[fx:2^(floor(log(w)/log(2)))]" $file)
if [ -z $powwidth ]
then
echo "$file" cannot be read
else
local powheight=$(identify -format "%[fx:2^(floor(log(h)/log(2)))]" $file)
mogrify -resize "$powwidth"x"$powheight" $file
echo $file resized to "$powwidth"x"$powheight"
fi
done
}
# Usage:
magickpow2 '*.png'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment