We Wish You a Meme Christmas
Imager - 2019-12-24
It's true. I've featured cat pictures and animated gifs. I've created Christmas playlists. I've shown you images of Christmas lights. I've even had an excuse to embedded entire Chistmas movies.
Now...it's finally time to create memes.
Imager
Imager is a handy dandy image drawing library in Perl that we're going to be using for our Meme creation needs.
Imager has about a gazillion methods to do pretty much every standard graphics manipulation you can imagine. But, shockingly, it doesn't have a way to draw standard meme text (Impact font, white text, black outline.) We can fix that by adding our own plugin:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: |
package Imager::MemeString;
use 5.024; use warnings; use experimental 'signatures';
use Imager (); use Imager::Font ();
sub Imager::meme_string( $img, %args) { my $string = uc delete $args{string};
my $font = Imager::Font->new( file => '/System/Library/Fonts/Supplemental/Impact.ttf', );
my $x = delete $args{x} // $img->getwidth / 2; my $y = delete $args{y};
my %defaults = ( font => $font, aa => 1, halign => 'center', );
for my $xoffset ( -3 .. 3 ) { for my $yoffset ( -3 .. 3) { $img->align_string( x => $x + $xoffset, y => $y + $yoffset, %defaults, %args, color => 'black', string => $string, ); } }
$img->align_string( x => $x, y => $y, %defaults, %args, color => 'white', string => $string, ); }
1; |
Now all we need is a wrapper script that loads our meme template, uses this new method to draw the meme text, and saves it out again:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: |
use 5.024; use warnings;
use Imager (); use Imager::MemeString();
my $template = shift or die 'Forgot to pass template filename!'; my $output = shift or die 'Forgot to pass output filename!';
my $img = Imager->new;
$img->read( file => $template ) or die $img->errstr;
$img->meme_string( string => "Hello, World", y => 50, size => 50 );
$img->write( file => $output, type => 'jpeg' ) or die "Cannot write: ",$img->errstr;
|
Okay, bring on the memes! (email me links to any you create!)

This article contributed by: Mark Fowler <mark@twoshortplanks.com>