Skip to content

Instantly share code, notes, and snippets.

@ww24
Last active October 24, 2016 01:53
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 ww24/fb244c70d691979b8e7d to your computer and use it in GitHub Desktop.
Save ww24/fb244c70d691979b8e7d to your computer and use it in GitHub Desktop.
ImageMagick を利用して、長方形の画像を一辺の長さを長辺に合わせた正方形に変換するシェルスクリプト
#!/bin/sh
if [ -z $1 ]; then
echo 'usage: minfix.sh image_path'
exit 1
fi
image_path=$1
min_width=511
min_height=375
background='white'
width=`identify -format "%w" $image_path`
height=`identify -format "%h" $image_path`
min_width=`[ $width -gt $min_width ] && echo $width || echo $min_width`
min_height=`[ $height -gt $min_height ] && echo $height || echo $min_height`
size="${min_width}x${min_height}"
echo "size:" $size
out_path="${image_path%.*}_$size.jpg"
echo "out:" $out_path
convert $image_path -resize $size -size $size xc:$background +swap -gravity center -composite $out_path
#!/bin/sh
if [ -z $1 ]; then
echo 'usage: square.sh image_path'
exit 1
fi
image_path=$1
background='white'
width=`identify -format "%w" $image_path`
height=`identify -format "%h" $image_path`
size=`[ $width -gt $height ] && echo $width || echo $height`
size="${size}x${size}"
echo "size:" $size
out_path="${image_path%.*}_square.jpg"
echo "out:" $out_path
convert $image_path -resize $size -size $size xc:$background +swap -gravity center -composite $out_path
@ww24
Copy link
Author

ww24 commented Apr 13, 2015

元画像と正方形の白色画像を重ね合わせて出力するイメージ。
よって、透過色は白色に置き換わる。

@ww24
Copy link
Author

ww24 commented Apr 13, 2015

minfix.shmin_width, min_height に合わせて元画像の比率を保ったまま変形するスクリプト。
縦横が min_height, min_width 以上になる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment