Last active
August 24, 2022 22:26
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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