2015-04-06 39 views
0

我想创建一个PNG文件。此脚本为什么在Windows上创建损坏的PNG文件?

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

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

我使用了Active Perl(5.18.2)和Strawberry Perl(5.18.4.1),但同样的问题。我尝试了草莓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; 

screenshot of cmd print

+3

你要得到什么?如果您不想显示二进制数据,请删除'print $ panel-> png;'行。或者移除'print OUTFILE $ panel-> png;'并使用script:'script.pl> file.png'。 – jm666

回答

2

你有

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; 
+0

yes'binmode OUTFILE'是我所缺少的。现在工作。感谢您的帮助 - 非常感谢,因为我还在学习语言(主要是通过错误,我发现!) – neemie