やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Linuxで画像ファイルの圧縮

Linuxで画像ファイルを圧縮したい。

https://compressor.io/

いつもこれを利用していた。

長所

  • 大抵60%は減らせる
  • インストール不要

短所

  • インターネット接続必須
  • サイトが異常に遅くなって使い物にならないことが割とある
  • D&DなどUI操作が必要

参考

http://morizyun.github.io/infrastructure/tool-optimize-jpeg-png.html

感謝。試してみる。

OptiPNG

PNG画像の圧縮。大抵PNGなので

$ sudo apt-get install optipng
[sudo] password for mint: 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下のパッケージが自動でインストールされましたが、もう必要とされていません:
  libc-ares2 libv8-3.14.5
これを削除するには 'apt-get autoremove' を利用してください。
以下のパッケージが新たにインストールされます:
  optipng
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 431 個。
78.0 kB のアーカイブを取得する必要があります。
この操作後に追加で 207 kB のディスク容量が消費されます。
取得:1 http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu/ trusty-updates/main optipng i386 0.6.4-1ubuntu0.14.04.1 [78.0 kB]
78.0 kB を 1秒 で取得しました (73.3 kB/s)
以前に未選択のパッケージ optipng を選択しています。
(データベースを読み込んでいます ... 現在 199515 個のファイルとディレクトリがインストールされています。)
.../optipng_0.6.4-1ubuntu0.14.04.1_i386.deb を展開する準備をしています ...
optipng (0.6.4-1ubuntu0.14.04.1) を展開しています...
doc-base (0.10.5) のトリガを処理しています ...
doc-base ファイルを 1 個追加 を処理中...
scrollkeeper をドキュメントとして登録しています...
man-db (2.6.7.1-1ubuntu1) のトリガを処理しています ...
optipng (0.6.4-1ubuntu0.14.04.1) を設定しています ...

使ってみた

optipng ファイルパスで実行した。

  • 1割も縮小できていない
  • 元ファイルを上書きされる?
$ optipng '/tmp/Screenshot at 2017-07-23 14:04:07.png' 
OptiPNG 0.6.4: Advanced PNG optimizer.
Copyright (C) 2001-2010 Cosmin Truta.

** Processing: /tmp/Screenshot at 2017-07-23 14:04:07.png
326x269 pixels, 4x8 bits/pixel, RGB+alpha
Input IDAT size = 5585 bytes
Input file size = 5694 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0     IDAT size = 5044
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0     IDAT size = 5044

Output IDAT size = 5044 bytes (541 bytes decrease)
Output file size = 5153 bytes (541 bytes = 9.50% decrease)

help

$ optipng -h
OptiPNG 0.6.4: Advanced PNG optimizer.
Copyright (C) 2001-2010 Cosmin Truta.

Synopsis:
    optipng [options] files ...
Files:
    Image files of type: PNG, BMP, GIF, PNM or TIFF
Basic options:
    -?, -h, -help   show this help
    -o <level>        optimization level (0-7)       default 2
    -v          verbose mode / show copyright and version info
General options:
    -fix        enable error recovery
    -force      enforce writing of a new output file
    -keep       keep a backup of the modified files
    -preserve       preserve file attributes if possible
    -quiet      quiet mode
    -simulate       simulation mode
    -snip       cut one image out of multi-image or animation files
    -out <file>       write output file to <file>
    -dir <directory>  write output file(s) to <directory>
    -log <file>       log messages to <file>
    --          stop option switch parsing
Optimization options:
    -f  <filters>   PNG delta filters (0-5)            default 0,5
    -i  <type>        PNG interlace type (0-1)     default <input>
    -zc <levels>  zlib compression levels (1-9)      default 9
    -zm <levels>  zlib memory levels (1-9)       default 8
    -zs <strategies>  zlib compression strategies (0-3)  default 0-3
    -zw <window size> zlib window size (32k,16k,8k,4k,2k,1k,512,256)
    -full       produce a full report on IDAT (might reduce speed)
    -nb         no bit depth reduction
    -nc         no color type reduction
    -np         no palette reduction
    -nx         no reductions
    -nz         no IDAT recoding
Optimization details:
    The optimization level presets
        -o0  <=>  -o1 -nx -nz
        -o1  <=>  [use the libpng heuristics]   (1 trial)
        -o2  <=>  -zc9 -zm8 -zs0-3 -f0,5   (8 trials)
        -o3  <=>  -zc9 -zm8-9 -zs0-3 -f0,5 (16 trials)
        -o4  <=>  -zc9 -zm8 -zs0-3 -f0-5    (24 trials)
        -o5  <=>  -zc9 -zm8-9 -zs0-3 -f0-5  (48 trials)
        -o6  <=>  -zc1-9 -zm8 -zs0-3 -f0-5  (120 trials)
        -o7  <=>  -zc1-9 -zm8-9 -zs0-3 -f0-5    (240 trials)
    The libpng heuristics
        -o1  <=>  -zc9 -zm8 -zs0 -f0        (if PLTE is present)
        -o1  <=>  -zc9 -zm8 -zs1 -f5        (if PLTE is not present)
    The most exhaustive search (not generally recommended)
      [no preset] -zc1-9 -zm1-9 -zs0-3 -f0-5    (1080 trials)
Examples:
    optipng file.png                (default speed)
    optipng -o5 file.png            (moderately slow)
    optipng -o7 file.png            (very slow)
    optipng -i1 -o7 -v -full -sim experiment.png

オプション

  • -o 7で最大圧縮する
  • -out 'test.png'で出力ファイルを指定する
$ optipng -o 7 -out 'test.png' '/tmp/Screenshot at 2017-07-23 14:04:07.gif' 
OptiPNG 0.6.4: Advanced PNG optimizer.
Copyright (C) 2001-2010 Cosmin Truta.

** Processing: /tmp/Screenshot at 2017-07-23 14:04:07.gif
Importing GIF
326x269 pixels, 8 bits/pixel, 256 colors (256 transparent) in palette
Input file size = 5593 bytes

Trying:
  zc = 9  zm = 9  zs = 0  f = 0     IDAT size = 1991
  zc = 9  zm = 8  zs = 0  f = 0     IDAT size = 1991
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0     IDAT size = 1991

Output file: test.png

Output IDAT size = 1991 bytes
Output file size = 3096 bytes (2497 bytes = 44.65% decrease)

今度は44.65%の圧縮に成功。https://compressor.io/には及ばないが、半分になるなら使う価値あり。

/tmpディレクトリ配下の*.pngをすべて圧縮する。

$ find /tmp -name '*.png' -print0 | xargs -0 optipng -o 7

ファイル名に半角スペースが入っていても大丈夫。

所感

各画像出力ツールに標準装備していて欲しい。