LoginSignup
22
24

More than 5 years have passed since last update.

Inkscapeで描いた画像をpngにバッチ変換する

Last updated at Posted at 2015-01-14

(と言っても、Inkscapeにバッチ出力機能があるわけではなく、シェルから叩けるのでスクリプト書いてしまえ、という話。)

こんにちは、あんどろいどりんご(@mstssk)です。

Inkcapeでわかめねこを描いたりAndroidアプリの画像リソースを描いたりしています。
描くのはいいんですが、各ファイルをdpiごとにエクスポートするのをメニューからポチポチやるのはめんどくさいですよね。
実は、Inkscapeはpngへのエクスポート程度なら、次の様にシェルからえいやとコマンド叩いてしまえます。

svgからpngへエクスポートする例
inkscape -z -f src.svg -e result.png

PATHを通す

インストール方法は割愛。
InkscapeがインストールされているディレクトリにPATHを通しておく。
Macの場合は /Applications/Inkscape.app/Contents/Resources/bin
WindowsやLinuxディストリとかは、インストール方法によって違うだろうからよしなに。

CUIからの実行

ヘルプ表示

--helpで使えるオプションが一覧できます。
png変換だけでなく、psやpdfにも出来るっぽいです。

ヘルプの表示
inkscape --help

PNGのエクスポート

-eまたは--export-png=でPNGへのエクスポート。書き出し先のファイルパスを指定します。
-fまたは--file=で入力ファイルの指定。
サイズを指定するオプションもあります。

xxxhdpi用ランチャーアイコンを書き出す例
inkscape -f ic_launcher.svg -e app/res/drawable-xxxhdpi/ic_launcher.png -w 192 -h 192

各DPIのアイコン画像を書き出すシェルスクリプト

絵文字マッシュで、タブ用アイコンをいくつも描いてるので一括で書き出すシェルスクリプト作りました。
アイコン用なので正方形前提。

オプション

  • n: dry-run。実行するコマンドをプレビューする。
  • o: 出力先ディレクトリ。デフォルトは./output/
  • s: (必須) 画像サイズ。mdpiでの画像サイズを指定します。他のdpiのサイズは比率で勝手に計算します。
    LauncherActionBarって書くと、ランチャー用(48dp)/ActionBar用(32dp)のサイズになります。
    ※TODO: Material Designでは32dpじゃなく24dp!
  • 元ファイルパス: svgファイルを指定する。同名のpngに書き出します。
    例えば、hoge.svgdrawable-xxxhdpi/hoge.pngに書き出します。
使い方
$ ./export-png.sh
Usage: ./export-png.sh [-n] [-o <output_dir>] -s <base_size> inkscape_files ...
$ ./export-png.sh -n -s launcher -o app/res ic_launcher.svg
inkscape -z -f ic_launcher.svg -e app/res/drawable-xxxhdpi/ic_launcher.png -w 192 -h 192
inkscape -z -f ic_launcher.svg -e app/res/drawable-xxhdpi/ic_launcher.png -w 144 -h 144
inkscape -z -f ic_launcher.svg -e app/res/drawable-xhdpi/ic_launcher.png -w 96 -h 96
inkscape -z -f ic_launcher.svg -e app/res/drawable-hdpi/ic_launcher.png -w 72 -h 72
inkscape -z -f ic_launcher.svg -e app/res/drawable-mdpi/ic_launcher.png -w 48 -h 48
inkscape -z -f ic_launcher.svg -e app/res/drawable-ldpi/ic_launcher.png -w 36 -h 36
$ ./export-png.sh -n -s actionbar -o app/res ic_tab_*.svg
inkscape -z -f ic_tab_flags.svg -e app/res/drawable-xxxhdpi/ic_tab_flags.png -w 128 -h 128
inkscape -z -f ic_tab_flags.svg -e app/res/drawable-xxhdpi/ic_tab_flags.png -w 96 -h 96
inkscape -z -f ic_tab_flags.svg -e app/res/drawable-xhdpi/ic_tab_flags.png -w 64 -h 64
inkscape -z -f ic_tab_flags.svg -e app/res/drawable-hdpi/ic_tab_flags.png -w 48 -h 48
inkscape -z -f ic_tab_flags.svg -e app/res/drawable-mdpi/ic_tab_flags.png -w 32 -h 32
inkscape -z -f ic_tab_flags.svg -e app/res/drawable-ldpi/ic_tab_flags.png -w 24 -h 24
...略...
22
24
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
22
24