【问题标题】:Perl using GD for graphic programming [closed]Perl 使用 GD 进行图形编程 [关闭]
【发布时间】:2013-06-16 21:48:50
【问题描述】:

gd.pl

#!/usr/bin/perl -w


use strict;
use GD;


my $image = GD::Image->newPalette(401,201);

my $gray = $image->colorAllocate(200,200,200);
my $red = $image->colorAllocate(255,0,0);
my $black = $image->colorAllocate(0,0,0);


#draw a field of polka dots with random diameters
foreach my $i (0..10) {
    foreach my $j (0..5) {
            my $d = rand(50)+1;
            $image->arc($i*40, $j*40, $d, $d, 0, 360, $red);
            $image->fill($i*40, $j*40, $red);
    }
}


#draw the text in black
my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = $image->stringFT($black, "/home/eugenep/arial.ttf", 48,0,40,120, "hello world");

#outline the text with a black box
$image->rectangle($x1-10, $y1+10, $x3+10, $y3-10, $black);

print $image->png;

它没有显示任何东西,而是在命令提示符下显示一堆奇怪的字符。

谁能告诉我可能是什么问题?

【问题讨论】:

  • 您是否尝试过将图像写入文件,然后用可以理解 PNG 的文件打开该文件?你的控制台只是在做你要求它做的事情。
  • 哦,有道理。我不知道 perl 是否类似于 java,当我执行 GD::Image->newPalette(401,201) @muistooshort 时它会自动创建 jframe ish

标签: perl gd


【解决方案1】:

没有错误。 “一堆奇怪的字符”是它生成的 PNG 文件,但你不能指望控制台显示图像。

将此脚本的输出保存到文件中(在 Linux 上:./yourscript.pl > file.png;在 Win 上您可能需要将 $image->png 保存到文件中),在浏览器或图像查看器中打开生成的文件,然后您会看到图片。

【讨论】:

  • 是否有将 $image 保存为 png 文件的 perl 代码版本?
  • 像往常一样打开它(作为二进制文件)并写入它。 PNG和其他任何信息一样都是信息。如果您不知道该怎么做,请参阅接受的答案 here 并将 pack('s<',255) 替换为 $image->png
  • 嗯,当我打开(my $out, '>:raw', 'sample.png') 或死“无法打开:$!”;打印 $out $image->png ;关闭($out);它只是给了我一张红色的图片:O
  • arc 替换为filledArc 并删除$image->fill(...)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
相关资源
最近更新 更多