ImagerでGIFファイルを書き出す
Mac OS X(10.11.16)でのメモ。
試してみたら、GIFをサポートしてないというエラーが出た。
$ perl aaa.pl
format 'gif' not supported - formats bmp, ico, jpeg, png, pnm, raw, sgi, tga, tiff available for writing - Can't locate Imager/File/GIF.pm at aaa.pl line 24.
そこで、Imager::File::GIFを入れようとしてみるもエラーになる。
“build.log”を見てみると、
Warning (mostly harmless): No library found for -lgif
OS unsupported: GIF libraries or headers not found
GIF: Test code failed: Can't link/include 'gif_lib.h', 'stdio.h', 'errno.h', 'string.h', 'gif'
っていうのが見つかったので、ライブラリが必要なのがわかる。
そこで、Homebrewでgiflibをインストールして事無きを得た。
$ brew install giflib
$ cpanm Imager::File::GIF
次に、パレットを用意して、画像ファイルを出力する。
use v5.14;
use strict;
use warnings;
use Imager;
my ( $w, $h ) = ( 640, 480 );
my $img = Imager->new(
xsize => $w, ysize => $h, channels => 1, type => 'paletted' );
my @colors = map {
Imager::Color->new( $_, $_, $_ );
} 0..255;
$img->addcolors( colors => \@colors );
my @samples = map {
my $tmp = $_ / ($w - 1);
int( (255 * $tmp) + 0.5 );
} 0..($w - 1);
for (my $iy=0; $iy<$h; $iy++) {
$img->setscanline(
y => $iy,
type => 'index',
pixels => \@samples );
}
#my @foo = $img->getscanline(
# y => 0,
# type => 'index' );
#say join(',', @foo);
my $dst_file = 'test.gif';
$img->write( file => $dst_file ) or die $img->errstr;
say 'wrote: ', $dst_file;
結果は、こんな感じ。
$ perl aaa.pl
wrote: test.gif
最初は、パレットの割り当てにsetcolorsを使ってうまくいかなくて困ってたけど、
割り当てはaddcolorsで、書き換えはsetcolorsらしい。
(setcolorsは使わず終いだったので。。。)
おしまい。

Leave a Comment