Skip to content

Instantly share code, notes, and snippets.

@greymd
Last active August 24, 2022 22:26
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 greymd/f3f010db74bc0f6933b6b8af35e136e9 to your computer and use it in GitHub Desktop.
Save greymd/f3f010db74bc0f6933b6b8af35e136e9 to your computer and use it in GitHub Desktop.
Shrink image, longest side 576 px, either side is ajusted to multiple of 64 px (for img2img)
#!/bin/bash
## Usage: adjust_image image.png
## => image.png will be shrinked.
## Require ImageMagick commands (convert, identify)
adjust_image() {
local imgpath="$1"
local outpath="$1"
longest_side=576
convert "$imgpath" -resize "$longest_side>" "$outpath"
read -r _width _height <<<"$(identify -format "%w %h" "$outpath")"
if (( _width % 64 > 0 ));then
(( _width = ( _width - ( _width % 64 ) ) ))
fi
if (( _height % 64 > 0 ));then
(( _height = ( _height - ( _height % 64 ) ) ))
fi
convert "$outpath" -gravity center -crop ${_width}x${_height}+0+0 "$outpath"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment