技ログ

Tech, etc.

OptiPNGでPNG形式の画像をロスレス圧縮・軽量化する

OptiPNGでPNG形式の画像を劣化なく圧縮し軽量化する。
OptiPNG公式

CentOSならyumでもインストール可能だが、バージョンが少し古いので、 ソースからインストールした。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ yum info optipng
Name        : optipng
Arch        : x86_64
Version     : 0.6.4
Release     : 1.el6
Size        : 82 k
Repo        : epel
Summary     : PNG optimizer and converter
URL         : http://optipng.sourceforge.net/
License     : zlib
Description : OptiPNG is a PNG optimizer that recompresses image files to a smaller size,
            : without losing any information. This program also converts external formats
            : (BMP, GIF, PNM and TIFF) to optimized PNG, and performs PNG integrity checks
            : and corrections.

ソースをダウンロード・展開。

1
2
3
$ wget "http://prdownloads.sourceforge.net/optipng/optipng-0.7.4.tar.gz?download"
$ tar xzf optipng-0.7.4.tar.gz
$ cd optipng-0.7.4

インストール方法確認。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[root@vagrant-blog optipng-0.7.4]# cat README.txt
Build instructions
------------------
  On Unix, or under a Bourne-compatible shell, run ./configure and make:
        cd optipng-0.7.4/
        ./configure
        make
        make test

Installation instructions
-------------------------
  Build the program according to the instructions above.

  On Unix:
  - Make the "install" target:
        sudo make install
  - To uninstall, make the "uninstall" target:
        sudo make uninstall

[root@vagrant-blog optipng-0.7.4]# ./configure --help
Usage:
    ./configure [options]
Options:
    -h, -help               Show this help
Installation directories:
    -prefix=PREFIX          Install architecture-independent files in PREFIX
                            [default: /usr/local]
    -exec-prefix=EPREFIX    Install architecture-dependent files in EPREFIX
                            [default: PREFIX]
    -bindir=DIR             Install executable in DIR [default: EPREFIX/bin]
    -mandir=DIR             Install manual in DIR [default: PREFIX/man]
Optional features:
    -enable-debug           Enable debug build flags and run-time checks
Optional packages:
    -with-system-libpng     Use the system-supplied libpng
                            [default: false]
    -with-system-zlib       Use the system-supplied zlib
                            [default: with-system-libpng]
Environment variables:
    CC                      C compiler command
    LD                      Linker command
    CFLAGS                  C compiler flags (e.g. -O3)
    CPPFLAGS                C preprocessor flags (e.g. -I DIR)
    LDFLAGS                 Linker flags (e.g. -L DIR)
    LIBS                    Additional libraries (e.g. -lfoo)

終了コードを確認しつつ、configure→make→make test→make installする。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ ./configure ; echo $?
0
$ CPU_CORE=$(grep -c '^processor\b' /proc/cpuinfo)
$ make -j $CPU_CORE ; echo $?
0
$ make test -j $CPU_CORE ; echo $?
0
$ sudo make install ; echo $?
cd src/optipng && \
  make install && \
  cd ../..
make[1]: Entering directory `/usr/local/src/optipng-0.7.4/src/optipng'
mkdir -p /usr/local/bin
mkdir -p /usr/local/man/man1
cp -f -p optipng /usr/local/bin
cp -f -p man/optipng.1 /usr/local/man/man1
make[1]: Leaving directory `/usr/local/src/optipng-0.7.4/src/optipng'
0

インストール確認。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ optipng -v
OptiPNG version 0.7.4
Copyright (C) 2001-2012 Cosmin Truta and the Contributing Authors.

This program is open-source software. See LICENSE for more details.

Portions of this software are based in part on the work of:
  Jean-loup Gailly and Mark Adler (zlib)
  Glenn Randers-Pehrson and the PNG Development Group (libpng)
  Miyasaka Masaru (BMP support)
  David Koblas (GIF support)

Using libpng version 1.4.12 and zlib version 1.2.7-optipng

オプション確認。
普通に使う分にはオプションなしでファイル指定するだけで圧縮してくれる。
→細かくオプションの内容を確認したい場合はman optipngする

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
$ optipng --help
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      run in verbose mode / show copyright and version info
General options:
    -backup, -keep  keep a backup of the modified files
    -clobber    overwrite existing files
    -fix    enable error recovery
    -force    enforce writing of a new output file
    -preserve   preserve file attributes if possible
    -quiet, -silent run in quiet mode
    -simulate   run in simulation mode
    -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)
    -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 <size>    zlib window size (256,512,1k,2k,4k,8k,16k,32k)
    -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
Editing options:
    -snip   cut one image out of multi-image or animation files
    -strip <objects>  strip metadata objects (e.g. "all")
Optimization levels:
    -o0   <=> -o1 -nx -nz       (0 or 1 trials)
    -o1   <=> -zc9 -zm8 -zs0 -f0      (1 trial)
        (or...) -zc9 -zm8 -zs1 -f5      (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)
    -o7 -zm1-9  <=> -zc1-9 -zm1-9 -zs0-3 -f0-5    (1080 trials)
Notes:
    The combination for -o1 is chosen heuristically.
    Exhaustive combinations such as "-o7 -zm1-9" are not generally recommended.
Examples:
    optipng file.png            (default speed)
    optipng -o5 file.png          (slow)
    optipng -o7 file.png          (very slow)

ここでは360KBの画像を圧縮してみる。
まずは-simulateオプションでどの程度圧縮されるか確認。
※実際には処理は行われない

→100KB程度圧縮される模様

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# optipng -simulate virtualbox-error-1.png
** Processing: virtualbox-error-1.png
1024x711 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 3x8 bits/pixel, RGB
Input IDAT size = 365449 bytes
Input file size = 368428 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0   IDAT size = 262703

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

No output: simulation mode.

-backupオプションでバックアップファイルを作成しつつ、圧縮してみる。

  • 27.96%圧縮され、約100KB軽量化
  • 処理時間は2秒程度(MacBook Pro Retina Late 2013 Core i5のVirtualbox上の仮想マシン)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ optipng -backup virtualbox-error-1.png
** Processing: virtualbox-error-1.png
1024x711 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 3x8 bits/pixel, RGB
Input IDAT size = 365449 bytes
Input file size = 368428 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0   IDAT size = 262703

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

Output IDAT size = 262703 bytes (102746 bytes decrease)
Output file size = 265418 bytes (103010 bytes = 27.96% decrease)

$ ll virtualbox-error-1.png*
-rw-r--r--  1  501 501  265418 2014-01-28 23:15 virtualbox-error-1.png
-rw-r--r--  1  501 501  368428 2014-01-28 23:30 virtualbox-error-1.png.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# time optipng -backup virtualbox-error-1.png
** Processing: virtualbox-error-1.png
1024x711 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 3x8 bits/pixel, RGB
Input IDAT size = 365449 bytes
Input file size = 368428 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0   IDAT size = 262703

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

Output IDAT size = 262703 bytes (102746 bytes decrease)
Output file size = 265418 bytes (103010 bytes = 27.96% decrease)


real  0m1.905s
user  0m1.900s
sys 0m0.003s
1
2
3
$ grep "model name" /proc/cpuinfo
model name  : Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz