LoginSignup
67

More than 3 years have passed since last update.

mac にImageMagickをインストールし、convertコマンドで画像を縮小する。

Last updated at Posted at 2015-12-29

内容

ImageMagickのconvertコマンドを使用し、大量にある大きな画像ファイルを、ホームページ用に小さい画像に変換する。
ImageMagick: Convert, Edit, Or Compose Bitmap Images
ImageMagick: Tools (how to use ImageMagick)

環境

今回はmacで作業を実施する。
▼ macのバージョン

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.1
BuildVersion:   15B42

手順

1. macにImageMagickをインストール

brewを使用してインストール。

▼ コマンド

brew install imagemagick

2. 動作確認

動作確認用の画像ファイル「testimg.png」を用意。
以下のコマンドで、50%と70%に縮小した画像ファイルを作成できることを確認する。

▼ コマンド

cd <testimg.pngファイルを格納しているディレクトリ>
# 画像を50%に縮小
convert -geometry 50% testimg.png testimg.50p.png
# 画像を70%縮小
convert -geometry 70% testimg.png testimg.70p.png

3. 一括処理

ディレクトリにある全てのpngファイルを、70%に縮小する例を記載する。
変換後のファイル名は以下のように"_70p"を付加する。
testimg.png → testimg _70p .png

▼ スクリプト

cd <画像ファイルを格納しているディレクトリ>
for fname in *.png; do
  newfname=$(echo "${fname}" | sed "s/\.png$/_70p.png/g")
  convert -geometry 70% "${fname}" "${newfname}"
done

以上!

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
67