やってみる

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

pngquantのバグが解決できない(error: cannot open *.png for reading)

 libpngのバグだとか、出力パスの権限だとかいうけど、対処しても解決できなかった。

問題

 pngquantコマンドを実行すると『稀に』以下のエラーが出て、圧縮ファイルの作成に失敗する。

  error: cannot open 20181128101730.png for reading

 問題の画像ファイルは以下。

f:id:ytyaru:20181128111843p:plain

原因

権限不足

 出力先ディレクトリの権限が不足しているからという。が、うまくいくときがほとんどなので関係ない。

libpngのバグ

 OSが導入しているlibpngが古いせい。libpng 1.2だとエラーが出るらしい。1.4以降が必要。

 Raspbianではaptでpngquantを入れられなかった。または古いものが入ってしまう。そこでソースコードからmakeした。このとき、昔 Raspbian jessie(8.0)でmakeした。おそらくそのOSのlibpngが1.2だったのだろう。それを今も実行ファイル単体をコピーして使い回していた。

$ pngquant -h
pngquant, 2.11.7 (January 2018), by Kornel Lesinski, Greg Roelofs.
   Compiled with no support for color profiles. Using libpng 1.2.54.

 libpngが古いものを参照している。これが原因だろう。

対処

 今はOSを新しくしてRaspbian stretch(9.0)になったので、たぶんlibpngも更新されているはず。再びpngquantのソースコードからmakeすれば解決するんじゃないか?

環境

  • Raspbierry pi 3 Model B+
  • Raspbian stretch 2018-06-27

libpng確認

$ apt list | grep libpng

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libpng++-dev/stable,stable,stable,stable 0.2.9-1 all
libpng-dev/stable,stable,stable,stable,now 1.6.28-1 armhf [インストール済み、自動]
libpng-sixlegs-java/stable,stable,stable,stable 2.0-1 all
libpng-sixlegs-java-doc/stable,stable,stable,stable 2.0-1 all
libpng-tools/stable,stable,stable,stable,now 1.6.28-1 armhf [インストール済み、自動]
libpng12-0/stable,stable,stable,stable,now 1.2.54-6 armhf [インストール済み、自動]
libpng12-dev/stable,stable,stable,stable 1.2.54-6 armhf
libpng16-16/stable,stable,stable,stable,now 1.6.28-1 armhf [インストール済み]
libpnglite-dev/stable,stable,stable,stable 0.1.17-2 armhf
libpnglite0/stable,stable,stable,stable 0.1.17-2 armhf

 すでにインストールされており、1.6だった。1.4より新しいので大丈夫そう。

pngquant

コンパイル

 コンパイルはメモリを消費するので各種プログラムを終了しておくこと。

apt-get install libpng-dev
git clone git://github.com/pornel/pngquant.git
cd pngquant
make

インストール

$ sudo make install
mkdir -p '/usr/local/bin'
mkdir -p '/usr/local/share/man/man1'
install -m 0755 -p 'pngquant' '/usr/local/bin/pngquant'
install -m 0644 -p 'pngquant.1' '/usr/local/share/man/man1/'

確認

$ pngquant
pngquant, 2.12.2 (November 2018), by Kornel Lesinski, Greg Roelofs.
   Compiled with no support for color profiles. Using libpng 1.6.28.
...

 新しくなった。libpngも新しい1.6を参照している。OK。

 だが、問題のファイルを圧縮しようとしたら同様のエラーが出た……。解決できず。

$ pngquant 20181128101730.png
  error: cannot open 20181128101730.png for reading

 どうすりゃいいの?