stabu has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

A little project I was doing is taking much longer than I thought, so I thought I would post here and see if anybody could point any way forward.

I want to take an SVG file and produce it at various magnifcations in PNG form. I'm using Image::LibRSVG for this batch file writing procedure, which is probably not what it was built for.
#!/usr/bin/perl -w use strict; use Image::LibRSVG; my $rsvg = new Image::LibRSVG(); my $infile=$ARGV[0]; for (my $i=1;$i<=10;$i++) { my $fs=sprintf("%05d.png",$i); $rsvg->convertAtZoom($infile,$fs,$i/10,$i/10); # be careful 1, is +normal zoom, not 100!!! undef $fs; } undef $rsvg;
What I would like is to not have the loop reading from file each time .. so I was thinking about referencing it as a filehandle and then dereferencing. A go or no-go area?

Idea is to make the code faster.

A bigger problem is that the output file is a different size each time. So I want to create a set canvas size and map the zoomed output to it. This can be done with GD, but again, Image::LibRSVG insists on saving to file each time. In fact it only has one saveAs method, and that's to write to file. Any ideas for me?

Image::LibRSVG is starting feels more like a wrapper, may be I should be looking into cairo for this.

Thanks for any ideas in advance. Cheers.

Replies are listed 'Best First'.
Re: SVG to many PNG problem
by zentara (Cardinal) on May 01, 2009 at 16:17 UTC
    Use Gtk2's SVG library. Then you can load the svg as a pixbuf, and manipulate, slice and dice, and convert to png. Here is a start. This autoresizes the svg when you resize the window.
    #!/usr/bin/perl use warnings; use strict; use Glib qw(TRUE FALSE); use Gnome2::Rsvg; Gtk2->init; my $img; my $mw = Gtk2::Window->new; $mw->signal_connect (delete_event => sub { Gtk2->main_quit }); my $vbox = Gtk2::VBox->new (FALSE, 4); $vbox->set_border_width (4); $vbox->show; # Create the main box my $vp = Gtk2::Viewport->new(undef, undef); # Layout the stuff $vbox->pack_start ($vp, TRUE, TRUE, 0); $vp->show; $mw->add( $vbox ); get_svg(400,400); $mw->signal_connect (event_after => \&event_after); #$mw->add_events ([qw/ button-press-mask # button-release-mask # key-release-mask # /]); #$mw->signal_connect('button-release-event' => sub { # my ($widget,$event) = @_; # #count is in the same namespace as the closure # print "release\n"; # }); #$mw->signal_connect (event => sub { # my ($item, $event) = @_; # warn "event ".$event->type."\n"; #return FALSE; #}); #$mw->signal_connect (realize => sub { #$window->move ($pos->{x}, $pos->{y}); #$window->resize ($pos->{width}, $pos->{height}); # print "1\n"; #}); $mw->show; Gtk2->main; ################################################3 sub get_svg { my ($x,$y) = @_; $vp->remove($img) if defined $img; my $file = 'svg.svg'; my $pb = Gnome2::Rsvg->pixbuf_from_file_at_zoom_with_max($file,15, +15,$x,$y); # ($file, x_zoom, y_zoom, max_x, max_y ) $img = Gtk2::Image->new_from_pixbuf($pb); #($x, $y) = ($pb->get_width, $pb->get_height); $vp->add($img); $img->show; #will auto expand ???? #$vp->set_size_request($x,$y); $vp->set_size_request(.9*$x,.9*$y); $vp->show; } ############################################ sub event_after { my ($mw, $event) = @_; return FALSE unless $event->type eq 'configure'; # print $event->type,"\n"; #return FALSE unless $event->button == 1; my ($x, $y) = $mw->get_size; print "$x $y\n"; get_svg($x,$y); return FALSE; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      Many thanks Zentara, that's appreciated.

      I will hack away at that. Cheers!
Re: SVG to many PNG problem
by JavaFan (Canon) on May 01, 2009 at 13:11 UTC
    A bigger problem is that the output file is a different size each time.
    Considering that you are taking a fixed vector graphic, and translating it to a compressed bitmapped format changing only the scale, getting different sized outputs is to be expected, isn't?
      that's an interesting comment. Yes, it shouldn't be a problem. It is quite definitely a feature of SVG, an essential key to its flexibility. So even more than expecting it, I should have been looking forward to it.

      I'm just looking for the right programming manner in which to express that flexibility. Many features become problems due to a context or situation with unaligned constraints. Choosing Image::LibRSVG and its comparatively small number of methods is just such a situation. And I mean my choice of the module, not the module itself ... I'm taking about the design decision.

      I'm not sure I should apologise though. It's just far too common a perspective to take. At best I was cavalier in calling it a problem. At worst, careless. I could have made two spelling mistakes instead, I don't think it would be graver.
        It is quite definitely a feature of SVG, an essential key to its flexibility.
        You've lost me here. Considering that you are writing PNG files, what does that have to do with SVG features?