hack のためのネタ帳, etc,,,

状況

画素数が多過ぎる画像を処理しようとすると以下のようなエラーが出て width or height exceeds limits とか言われ処理に失敗する。
$ convert hoge_2400dpi.jpg -density 600 -units PixelsPerInch hoge_600dpi.jpg 
convert hoge_2400dpi.jpg -density 600 -units PixelsPerInch hoge_600dpi.jpg 
convert-im6.q16: width or height exceeds limit `hoge_2400dpi.jpg' @ error/cache.c/OpenPixelCache/3839.
convert-im6.q16: no images defined `hoge_600dpi.jpg' @ error/convert.c/ConvertImageCommand/3258.
convert-im6.q16: width or height exceeds limit」でググってみると、
以下のようにすると上限値が確認出来るらしい。
$ convert -list resource
Resource limits:
  Width: 16KP
  Height: 16KP
  Area: 128MP
  Memory: 256MiB
  Map: 512MiB
  Disk: 1GiB
  File: 768
  Thread: 8
  Throttle: 0
  Time: unlimited
と言うことで、処理対象のファイルが、
$ identify hoge_2400dpi.jpg 
hoge_2400dpi JPEG 16614x11807 16614x11807+0+0 8-bit sRGB 9.474MB 0.000u 0:00.000
のように width が 16KP 越えしていたのでこれに引っかかっていた模様。

これら limit の値は、環境変数 MAGICK_${PROP}_LIMIT とか -limit $PROP $VALUE オプションとか、policy.xml でこの値を設定出来るらしい。
ところが、
$ convert -limit WIDTH 100 -list resource | grep -i width
  Width: 100P
$ convert -limit WIDTH 100000 -list resource | grep -i width
  Width: 16KP
$ MAGICK_WIDTH_LIMIT=100 convert -list resource | grep -i width
  Width: 100P
$ MAGICK_WIDTH_LIMIT=100000 convert -list resource | grep -i width
  Width: 16KP
のように、元々の limit よりも小さい値へは変更できるが大きな値へな変更出来ない。

仕方がないので、
cp  /etc/ImageMagick-6/policy.xml ~/.config/ImageMagick/pilicy.xml
として ~/.config/ImageMagick/pilicy.xml の値を増やしてみたが、これも効果なし。

結局、/etc/ImageMagick-6/policy.xml の値を増やすと、上限値を増やすことが出来た。
どうも、よりシステム側の limit が最大値として制約になっており、それを越える値には設定が出来ないということらしい。

DPI 変換

やりたかったことはこっちなのだが、
物理的な紙面サイズ(つまり、inch とか cm とか mm)はそのままで 2400DPI の画像を 600DPI に変換したい。
これは、-density 600 -units PixelsPerInch で行けるかなと思ったら、これだと、DPI は変更されるが width x height の画素数はそのままだった。
そこで -resize 25% 付けてみたらこれも効果無くて、結局 -scale 25% を付けると目的が達成出来た。

ただし、width だけ増やすと、
$ convert hoge_2400dpi.jpg -density 600 -units PixelsPerInch hoge_600dpi.jpg
convert-im6.q16: DistributedPixelCache '127.0.0.1' @ error/distribute-cache.c/ConnectPixelCacheServer/244.
convert-im6.q16: cache resources exhausted `hoge_2400dpi.jpg' @ error/cache.c/OpenPixelCache/3984.
convert-im6.q16: no images defined `hoge_600dpi.jpg' @ error/convert.c/ConvertImageCommand/3258.
のように cache resources exhausted と言われてエラーになったので、disk の値も増やす必要があった。

しかし、user, sys に比べて real の時間が10倍以上かかってしまう。
$ time convert hoge_2400dpi.jpg -density 600 -scale 25% -units PixelsPerInch hoge_600dpi.jpg

real	0m39.457s
user	0m3.056s
sys	0m0.821s
area, memory, map 等も増やしてみたが効果が見られなかった。

空きメモリは充分なはずなので、swap ではないと思うのだが、なんだこれは?
$ free -g
              total        used        free      shared  buff/cache   available
Mem:             28          19           7           1           1           7
Swap:            46           0          46

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます