金子邦彦研究室インストールオープンデータ,データファイル処理画像ファイルのコマンド等

画像ファイルのコマンド等

コマンド等を用いて画像ファイルの確認や処理を行う手順を紹介.

1. 前準備

前準備

ImageMagick のインストール

curl のインストール

ここで説明のためにサンプルとして使用する画像

curl コマンドを用いてダウンロードできる.あるいは Web ブラウザでもダウンロードできる.

curl -L https://github.com/opencv/opencv/blob/master/samples/data/fruits.jpg?raw=true -o fruits.jpg

2. 画像ファイルの情報取得

ImageMagick の identify コマンドによる情報の取得

静止画像の情報の取得には, ImageMagick のidentify コマンドが便利.

※ 参考 Web ページ: http://www.imagemagick.org/script/escape.php

identify -format "%k" fruits.jpg
identify -format "%q" fruits.jpg
identify -format "%r" fruits.jpg
identify -format "%w" fruits.jpg
identify -format "%h" fruits.jpg
identify -format "%x" fruits.jpg
identify -format "%y" fruits.jpg
identify -format "%z" fruits.jpg

Ubuntu での実行結果

[image]

3. 静止画の形式変換,拡大縮小,切り出し

ImageMagick を用いた静止画の形式変換

次のように ImageMagick の 「convert」を使う.

convert <画像ファイル名> <変換後の画像ファイル名>

ImageMagick を用いて画像ファイルを PDF に変換する

次のように ImageMagick の 「convert」を使う.

convert <画像ファイル名> <変換後の PDF ファイル名>

ImageMagick を用いた静止画の拡大縮小

次のように ImageMagick の 「convert -resize」を使う.

convert -resize ...

あるディレクトリ内の全画像ファイルについて、サイズを変えた別の画像を作りたい場合。

bash スクリプト

for i in *.jpg; do 
  echo $i
  convert -resize 256 $i `basename $i .jpg`_thumb.png
done

ImageMagick の切り出し

次のように ImageMagick の 「convert -crop」を使う.

convert -crop ...

例えば「convert -crop +0+200 fruits.jpg a.jpg」のように実行する.