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

What module(s) would you recommend to generate icons on the fly from simple text of some font and color?

I need this because I have a system tray watchdog application the sole output of which is a decimal number. Currently I display this using a Balloon which pops up when the number changes however I find this annoying. I would like to show this information in the tray icon itself.

Replies are listed 'Best First'.
Re: Icon generation on the fly
by Khen1950fx (Canon) on Jan 31, 2012 at 13:22 UTC
    SystemTray::Applet::Gnome would work for a simple decimal number. It's built over Gtk2 and sets up Gtk2::TrayIcon for you. Right click to "Quit".
    #!/usr/bin/perl use strict; use warnings; use SystemTray::Applet::Gnome; my $num = 10; my $applet = SystemTray::Applet::Gnome->new( "text" => $num, "callback" => sub { my ($self) = @_; $self->{"text"} = $num; } );
      Thanks a lot but I'm incapable of building Gtk2. It may be possible but seems not to worth the effort as of yet. I would rather put something to work with the means easily accesible. With Imager suggested by Anonymous Monk I was able to put together this code:
      use Imager; my $icon_width=128; my $icon_height=128; my $image = Imager->new(xsize => $icon_width, ysize => $icon_height); $image->box( xmin=>0, ymin=>0, xmax=>$icon_width-1, ymax => $icon_height-1, filled => 1, color => '#ffffff' ); my $font_filename = 'C:/Windows/Fonts/arialbd.ttf'; my $font = Imager::Font->new(file=>$font_filename) or die "Cannot loa +d $font_filename: ", Imager->errstr; print "Enter number: ";my $text=<>;chomp($text); my $text_size = 96; $font->align( string => $text, size => $text_size, color => '#000000', x => $image->getwidth/2, y => $image->getheight/2, halign => 'center', valign => 'center', image => $image ); $image->write(file=>'number.ico') or die 'Cannot save image: ', $image +->errstr;
      It is a little bit weird (my be this is not the right font for this) but it works.
        If you can't build Gtk2, then you could use SystemTray::Applet. It doesn't require Gtk2, and it doessn't require any specialized knowledge of the user's toolkit.
        #!/usr/bin/perl use strict; use warnings; use SystemTray::Applet; my $num = 10; my $applet = SystemTray::Applet->new( "text" => $num, "callback" => sub { my ($self) = @_; $self->{"text"} = $num; } );
Re: Icon generation on the fly
by zentara (Cardinal) on Jan 31, 2012 at 17:47 UTC
    Does this look interesting? Use a here doc to define an XPM, then convert to icon.
    #!/usr/bin/perl use warnings; use strict; use Tk; # by Ch Lambrecht my $logo = <<'logo_end'; /* XPM */ static char * logo_xpm[] = { "32 32 5 1", " c None", ". c #FD000A", "+ c #FD6463", "@ c #FEBEBE", "# c #FEFFFC", "################################", "################################", "################################", "################################", "################################", "################################", "################################", "################################", "################################", "########........##.....@########", "#########@....+####@.###########", "##########+....@###.@###########", "###########.....##++############", "###########@....+@.#############", "############+.....@#############", "#############.....##############", "#############@....@#############", "#############@.....#############", "#############.+....+############", "############++#+....@###########", "###########@.###.....###########", "###########.@###@....+##########", "##########++#####+....@#########", "########+....###........########", "################################", "################################", "################################", "################################", "################################", "################################", "################################", "################################"}; logo_end ; package Tk::Toplevel; sub InitObject{ my ($self,$args) = @_; $self->SUPER::InitObject($args); my $mw = $self; while ($mw->parent){$mw = $mw->parent} if ($mw->cget('-appicon')){ $self->afterIdle(sub{$self->iconimage($mw->cget('-appicon'))} +) ; } } package MainWindow; sub Populate{ my ($self,$args) = @_; $self->SUPER::Populate($args); $self->ConfigSpecs(-appicondata => ['METHOD'], -appiconfile => ['METHOD'], -appicon => ['PASSIVE'] ); $self->configure(%$args); } sub appicondata{ my $self = shift; $self->configure(-appicon => $self->Pixmap(-data=>$_[0])); } sub appiconfile{ my $self = shift; $self->configure('-appicon' => $self->Pixmap(-file=>$_[0])); } package main; #my $mw = MainWindow->new(-appiconfile => 'logo.xpm'); my $mw = MainWindow->new(-appicondata => $logo); $mw->Toplevel(); my $tp = $mw->Toplevel(); MainLoop;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      This app seems to override some Tk methods. Is it possible in your view to create a windowless Toplevel ( I mean with no edges, title bar etc. just a plain widget: say a canvas )?
        mean with no edges, title bar etc. just a plain widget: say a canvas

        Something like this?

        #!/usr/bin/perl -w use warnings; use strict; use Tk; use Tk::LabEntry; my $mw = MainWindow->new; my $vh = $mw->vrootheight; my $vw = $mw->vrootwidth; # this is what grabs all virtual desktops $mw->overrideredirect(1); # Note that the 'virtual window' height and width are $vh and $vw # respectively, so we use those dimensions for our Canvas height # and width, and let the Canvas expand and fill in both x and y # directions. # my $canvas = $mw->Canvas( -width => $vw, -height => $vh, -background =>'blue', -takefocus =>0 ); # so canvas dosn't take focus on tab press $canvas->pack(-expand => 1, -fill => 'both'); #just for fun instead of an image $canvas->createRectangle(100, 100, 150, 150, -fill => 'orange'); my $window = $canvas->Button(-text=> 'Ok', -command=> sub{ exit;} ); $canvas->createWindow($vw/2, $vh/2, -window=> $window ); MainLoop;

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Icon generation on the fly
by Anonymous Monk on Jan 31, 2012 at 11:17 UTC

    None :)

    You can write text in the trayicon , they're not limited to images (though some perl modules you may have chosen may appear to be limited to icons, only because they're written that way); you're not limited to baloons

    Gtk2::TrayIcon ought to work for you

      Simple/fast would be to create xbm (X BitMap) and store in hash , its basically ascii art (pixel plotting)

      No Gtk2 for godness' sake. Apart from this I'm interested in icon generation with some algorythm rather than drawing it by hand.

        No Gtk2 for godness' sake.

        Why, its a finished and working solution, no need to reimplement.

        Apart from this I'm interested in icon generation with some algorythm rather than drawing it by hand.

        Sure you're not :)

        GD,Imager, ImageMagick, Image::TextMode ...