#!/usr/bin/perl # Animated Heatmap # v1. https://www.perlmonks.org/index.pl?node_id=11104262 # v2. https://www.perlmonks.org/index.pl?node_id=11104285 # v3. This; feel free to hack it up and post your version! use strict; use warnings; use Imager; use Imager::Filter::Flines; use Imager::Heatmap; # Configuration my $size = { x => 600, y => 60 }; my $japh = 'Just another Perl hacker'; my $filename = 'heatmap_anon_bliako.gif'; # Figure out font my $fontface = $^O eq 'MSWin' ? 'Arial' : $^O eq 'darwin' ? '/System/Library/Fonts/Keyboard.ttf' : '/usr/share/fonts/open-sans/OpenSans-Semibold.ttf'; my $font = $^O eq 'MSWin' ? Imager::Font->new( face => $fontface, size => $size->{x}/12, aa => 1) : Imager::Font->new( file => $fontface, size => $size->{x}/12, aa => 1); STDOUT->autoflush(1); print "Generating GIF frame "; # Create the image my $text = Imager->new(xsize=>$size->{x}, ysize=>$size->{y}); # Generate the text $text->box(color => Imager::Color->new(255, 255, 255), filled => 1); $text->string( font => $font, text => $japh, color => Imager::Color->new('#000000'), x => 1, y => ($size->{x}/12)-1, ); my @insert = (); # Scan image of text for heatmap data for my $x (0..$size->{x}-1) { for my $y (0..$size->{y}-1) { my $pix = ($text->getpixel(x=>$x, y=>$y, type=>'8bit')->rgba())[0]; push @insert, [ $x, $y, $pix ] } } my @images = (); # Generate, draw and filter the heatmap for my $x (1..10) { my $hmap = Imager::Heatmap->new( xsize => $size->{x}, ysize => $size->{y}, xsigma => $x, ysigma => $x, ); $hmap->insert_datas(@insert); $hmap = $hmap->draw; $hmap->filter(type => 'flines'); push @images, $hmap; print "$x " } print "done!\n"; # Write animated gif Imager->write_multi({ file => $filename, transp => 'none', gif_loop => 0, }, @images); # Isn't perl wonderful? print "Saved $filename\n";