M
Mart van de Wege
Hi,
I'm trying to convert some graphs generated by SVG::TT::Graph to PNG,
using Image::Magick. However, I'm getting empty .png files back,
instead of PNG images.
This is the code I am using:
sub output {
my $self = shift;
my $outdir = $self->{OutDir};
die "No output directory set." unless $self->{OutDir};
my $tempdir = tempdir;
foreach my $graph( keys %{$self->{Graphs}} ) {
my $svg = $self->{Graphs}->{$graph};
print "converting $graph, using tempdir $tempdir\n";
open(my $fh,">","$tempdir/$graph.svg") or die "$!";
print $fh $svg;
close $fh;
open(FH,"<","$tempdir/$graph.svg") or die "$!";
my $image = Image::Magick->new(magick => 'svg');
$image->Read( file => \*FH );
close FH;
my $out_filename = "$graph.png";
chdir $outdir;
open(IMG_FILE,">","$graph.png") or die "$!";
$image->Write( { file => \*IMG_FILE,
filename => "$graph.png",
magick => 'png' });
close IMG_FILE;
}
}
(Note, I am using bareword filehandles because I thought it might make a
difference with an older module like Image::Magick. It doesn't work with
lexical handles either.)
A separate script that takes just a filename from the command line works
just fine:
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $infile=shift;
my ($basename,$filetype)=split(/\./,$infile);
open(INFILE,"<",$infile) or die "$!";
my $image=Image::Magick->new;
$image->Read(file=>\*INFILE);
$image->Set(density=>'300x300');
my $outfile=$basename . ".png";
open(OUTFILE,">",$outfile);
$image->Write(file=>\*OUTFILE, filename=>$outfile);
close(INFILE);
close(OUTFILE);
What is the difference I am overlooking here? Or should I just switch to
Image::LibRSVG?
Mart
I'm trying to convert some graphs generated by SVG::TT::Graph to PNG,
using Image::Magick. However, I'm getting empty .png files back,
instead of PNG images.
This is the code I am using:
sub output {
my $self = shift;
my $outdir = $self->{OutDir};
die "No output directory set." unless $self->{OutDir};
my $tempdir = tempdir;
foreach my $graph( keys %{$self->{Graphs}} ) {
my $svg = $self->{Graphs}->{$graph};
print "converting $graph, using tempdir $tempdir\n";
open(my $fh,">","$tempdir/$graph.svg") or die "$!";
print $fh $svg;
close $fh;
open(FH,"<","$tempdir/$graph.svg") or die "$!";
my $image = Image::Magick->new(magick => 'svg');
$image->Read( file => \*FH );
close FH;
my $out_filename = "$graph.png";
chdir $outdir;
open(IMG_FILE,">","$graph.png") or die "$!";
$image->Write( { file => \*IMG_FILE,
filename => "$graph.png",
magick => 'png' });
close IMG_FILE;
}
}
(Note, I am using bareword filehandles because I thought it might make a
difference with an older module like Image::Magick. It doesn't work with
lexical handles either.)
A separate script that takes just a filename from the command line works
just fine:
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $infile=shift;
my ($basename,$filetype)=split(/\./,$infile);
open(INFILE,"<",$infile) or die "$!";
my $image=Image::Magick->new;
$image->Read(file=>\*INFILE);
$image->Set(density=>'300x300');
my $outfile=$basename . ".png";
open(OUTFILE,">",$outfile);
$image->Write(file=>\*OUTFILE, filename=>$outfile);
close(INFILE);
close(OUTFILE);
What is the difference I am overlooking here? Or should I just switch to
Image::LibRSVG?
Mart