perlのimager で、背景などの透過PNGは、作成できますか?

できたら教えてください。

回答の条件
  • 1人5回まで
  • 登録:
  • 終了:2013/03/31 13:13:54

ベストアンサー

id:TransFreeBSD No.1

回答回数668ベストアンサー獲得回数268

http://search.cpan.org/perldoc?Imager::Cookbook#Transparent_PNG

To make a transparent image, create an image object with 2 or 4 channels:

  # RGB with alpha channel
  my $rgba = Imager->new(xsize => $width, ysize => $height, channels => 4);

  # Gray with alpha channel
  my $graya = Imager->new(xsize => $width, ysize => $height, channels => 2);

By default, the created image will be transparent.

最後の行にあれっ?と思ったが、デフォルトのチャンネルは3、つまり透過を表すアルファチャンネルがないのは確か。
http://search.cpan.org/perldoc?Imager::ImageTypes#Creating_Imager_Objects

channels - The number of channels for the image. Default 3. Valid values are from 1 to 4.

たぶん、アルファチャンネル付きだと、作成時は全域が透過状態という意味なのでしょう。

他1件のコメントを見る
id:TransFreeBSD

特に追加しなければならないモジュールはないと思います。
私の所では以下で周囲が透明なpng画像が出来ています。

use strict;
use warnings;
use Imager;

my $image = Imager->new(xsize => 100, ysize => 100, , channels => 4);
$image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
	    filled => 1, color => 'green');
$image->write(file=>'box.png')
	or die 'Cannot save box.png: ', $image->errstr;
2013/03/31 00:30:39
id:kamesuta

#!/usr/bin/perl

use Imager;
my $string = "ああああ";
my $font = Imager::Font->new(
file => '/home/XXXX/public_html/font/font001.TTF',
color => '#000000',
size => 60,
);
my $bbox = $font->bounding_box(string => $string); # 文字情報を取得
my $img = Imager->new(xsize => $bbox->total_width*1.1, ysize => 70, ,channels => 4);←これを付けて、、

#↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
#$img->box(filled => 1);←←これを消したらできました。
#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


$img->string(
string => $string,
utf8 => 1,
font => $font,
x => 0,
y => 60,
aa => 1,
);
print "Content-type: image/png\n\n";
print $img->write(fh => \*STDOUT, type => 'png') or die $img->errstr;

変なボックスを描画していたからできなかったのですね。
ありがとうございます

2013/03/31 13:13:00

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません