Perl:将(x,y)像素绘制到单色位图的推荐方法

发布于 2024-07-29 18:53:07 字数 834 浏览 3 评论 0原文

对于家庭项目,我需要将 (x,y) 坐标绘制到 400x400 黑白位图上。

您会推荐哪种 perl 模块?哪种图像格式(GIF?、PNG?其他?)最容易在 OS X、Windows、Linux 上处理?


编辑 我的解决方案,基于 GD,由 Brian Agnew 推荐


use strict;
use warnings;
use GD;
my $BitMap = GD::Image->new(400,400);

my $white = $BitMap->colorAllocate(255,255,255);
my $black = $BitMap->colorAllocate(0,0,0);       

# Frame the BitMap
$BitMap->rectangle(0,0,399,399,$black);
# Transparent image, white background color
$BitMap->transparent($white);

# plot some, just to show it works #
for my $x (0..100) {
   for my $y (0 .. 100) {
      $BitMap->setPixel(250+100*sin($x)-$y,150+125*cos($x)+$y,$black); 
   }
}

# write png-format to file
open my $fh,">","test.png" or die "$!";
binmode $fh;
print $fh $BitMap->png;
close($fh);

For a home project I need to plot (x,y) coordinates onto a 400x400 black and white-bitmap.

What perl module would you recoment and what image format (GIF?, PNG? other?) would be easiest to handle on OS X, Windows, Linux?


EDIT My solution, based on GD, as recomended by Brian Agnew


use strict;
use warnings;
use GD;
my $BitMap = GD::Image->new(400,400);

my $white = $BitMap->colorAllocate(255,255,255);
my $black = $BitMap->colorAllocate(0,0,0);       

# Frame the BitMap
$BitMap->rectangle(0,0,399,399,$black);
# Transparent image, white background color
$BitMap->transparent($white);

# plot some, just to show it works #
for my $x (0..100) {
   for my $y (0 .. 100) {
      $BitMap->setPixel(250+100*sin($x)-$y,150+125*cos($x)+$y,$black); 
   }
}

# write png-format to file
open my $fh,">","test.png" or die "$!";
binmode $fh;
print $fh $BitMap->png;
close($fh);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-08-05 18:53:07

查看GD 模块(它与GD 库)。 它使创建图形变得非常简单,并且具有多种输出格式,包括 PNG 和 GIF。

Have a look at the GD module (which interfaces to the GD library). It makes creating graphics pretty trivial and has a wide range of output formats, including PNG and GIF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文