【问题标题】:Why is this script creating a corrupted PNG file on Windows?为什么此脚本会在 Windows 上创建损坏的 PNG 文件?
【发布时间】:2015-04-06 14:41:48
【问题描述】:

我正在尝试创建一个 PNG 文件。

下面的脚本执行没有返回任何错误,无法查看输出文件tester.png(并且cmd窗口打印附加的附加文本)。

我不确定为什么我无法查看此脚本生成的 PNG 文件。

我使用过 Active Perl (5.18.2) 和 Strawberry Perl (5.18.4.1) 但同样的问题。我尝试了 Strawberry Perl,因为它有 libgdlibpng 作为安装的一部分,即使我没有收到任何错误。有什么建议吗?

#!/usr/bin/perl

use Bio::Graphics;
use Bio::SeqFeature::Generic;
use strict;
use warnings;

my $infile = "data1.txt";
open( ALIGN, "$infile" ) or die;
my $outputfile = "tester.png";
open( OUTFILE, ">$outputfile" ) or die;

my $panel = Bio::Graphics::Panel->new(
    -length => 1000,
    -width  => 800
);
my $track = $panel->add_track(
    -glyph => 'generic',
    -label => 1
);

while (<ALIGN>) {    # read blast file
    chomp;

    #next if /^\#/;  # ignore comments
    my ( $name, $score, $start, $end ) = split /\t+/;
    my $feature = Bio::SeqFeature::Generic->new(
        -display_name => $name,
        -score        => $score,
        -start        => $start,
        -end          => $end
    );
    $track->add_feature($feature);

}

binmode STDOUT;
print $panel->png;
print OUTFILE $panel->png;

【问题讨论】:

  • 你想得到什么?如果您不想显示二进制数据,请删除 print $panel-&gt;png; 行。或删除print OUTFILE $panel-&gt;png; 并将脚本用作:script.pl &gt; file.png

标签: perl png bioperl


【解决方案1】:

你有

binmode STDOUT;
print $panel->png;

有趣的是,你还有:

print OUTFILE $panel->png;

但你从来没有binmode OUTFILE。因此,您在命令提示符中显示 PNG 文件的内容,并创建损坏的 PNG 文件。 (另见When bits don't stick。)

如果您删除 print OUTFILE ...,并将脚本的输出重定向到 PNG 文件,您应该能够在图像查看器中查看其内容。

C:\> perl myscript.pl > panel.png

或者,您可以避免将二进制文件的内容打印到控制台窗口,而是使用

binmode OUTFILE;
print $panel->png;

【讨论】:

  • 是的 'binmode OUTFILE' 是我所缺少的。现在工作。感谢您的帮助 - 非常感谢,因为我仍在学习这门语言(主要是因为错误,我发现!)
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多