LoginSignup
0
1

More than 5 years have passed since last update.

GIMP / ImageMagick / bash > 複数画像から同じ領域を切り取り続ける方法

Last updated at Posted at 2017-02-01
動作環境
CentOS 6.8 (64bit)
ImageMagick 6.7.2-7 2016-06-16 Q16 http://www.imagemagick.org
GIMP 2.6.9

複数画像から同じ領域を切り取り続けたい。
1つ1つ手動で切り取りをする作業を120枚程度の画像に対してしていたら日が暮れる。

手順

例えば以下の場合、始点[50,50]からサイズ[100x200]の領域を切り取る。

convert -crop 100x200+50+50 Main.png 2.png

bash script

複数画像を処理するスクリプト

v0.2

loop_crop_170201_exec
#!/usr/bin/env bash

# v0.2 Feb.01,2017
#  - use [start] and [end] instead of [width],[height] for runtime parameters
# v0.1 Feb.01,2017
#  - crop based on the runtime parameters
#

if [ $# -lt 4 ]; then
    echo "Usage:"
    echo "  [cmd] [start_x] [start_y] [end_x] [end_y]"
    echo "   to crop all the png images in this directory"
    echo 
    exit
fi

width=$(($3-$1+1))
height=$(($4-$2+1))

RESULT_DIR="RESULT"
mkdir ${RESULT_DIR}

for afile in $(ls *.png);do
    echo "[$afile] processed"
    convert -crop ${width}x${height}+$1+$2 $afile ${RESULT_DIR}/$afile
done

実行例 > 引数なし > 使い方の表示

$ bash loop_crop_170201_exec 
Usage:
  [cmd] [start_x] [start_y] [end_x] [end_y]
   to crop all the png images in this directory

実行例 > 引数指定

$ bash loop_crop_170201_exec 799 373 1285 658
[160705.png] processed
[160706.png] processed
[160707.png] processed
[160708.png] processed
[160709.png] processed

eog RESULT/160705.png
にてきちんと切り出しできたか確認する。

余談

使っているツールの機能で、上記の処理を簡単にできることが分かったのは数秒前だ。

お蔵入りscriptがまた一つ。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1