Re: [PerlChina] 请教熟悉图像处理的朋友

cnhack TNT cnhacktnt at gmail.com
Fri Jul 29 13:57:24 PDT 2005


用 GD 模块是可以做的,但是我对该模块也不是很熟悉,刚刚看了会儿它的文档,做了下试验,代码如下
-------------------------
#!/usr/bin/perl
# cnhackTNT _at_ perlchina.org
# www.perlchina.org

use GD;

$img = GD::Image->new('flip.gif');
$green = $img->colorAllocateAlpha(0,255,0,127); #这句设置透明绿色
$img->fill(137,13,$green);
open(IMG,">res.gif");
binmode IMG;
print IMG $img->gif;
close IMG;
-------------------------

附件中的 res.gif 即为效果图,flip.gif为未填充图

但是,上面那个gd的例子似乎不完美,实际上处理图片最牛逼的模块是 Image::Magick,结合 GD,我们用 Image::Magick
来试试,我对此模块也不是很熟悉,刚刚看了会儿文档,用种笨方法,即用gd生成一个透明绿色的图片,然后用  Image::Magick
将其与未填充图合并,效果如附件中flip-green.png 所示,基本达到要求.

代码如下:
-----------------------
#!/usr/bin/perl
# cnhackTNT<at>perlchina.org
# www.perlchina.org
use GD;
use Image::Magick;

$img = GD::Image -> new( 40,25 );
$image = new Image::Magick;
$image2 = new Image::Magick;

$green = $img -> colorAllocateAlpha( 0,255,0,90 ); #这句设置透明绿色
$img -> fill( 0,0,$green );
open( IMG,">green.png" );
print IMG $img -> png;
close IMG;
$image -> Read( 'flip.gif' );
$image2 -> Read( 'green.png' );
$image -> Composite( image => $image2,x => '137',y => '13' );
$image -> Write( 'flip-green.png' );

-----------------------
关于 gd 和 的相关方法调用,请参看:

http://search.cpan.org/~lds/GD-2.25/GD.pm.PLS

http://www.imagemagick.org/script/perl-magick.php


希望能对你有些许帮助,也许你自习看过文档找过资料后会有聪明的办法,不像我这个呵呵.




On 7/29/05, Jester <jester at perlchina.org> wrote:
> 
> 哪位做过类似我的附件里面的图形中的这样的处理?
> 也就是在一个指定的区域中打上底色,而且要求保留原来的文字。
> 我只会用GD,但是里面没有这样的方法。单是fill肯定不行,因为有些文字有圈的,会fill不上的……
> 不知道有没有哪位知道用什么方法?或者有什么模块可以做的?
> _______________________________________________
> China-pm mailing list
> China-pm at pm.org
> http://mail.pm.org/mailman/listinfo/china-pm
> 
>


More information about the China-pm mailing list