Using Perl and ImageMagick to Generate Meme Image Macros

Sometimes I feel a need to automate some meme generation. Luckily I found this incredibly helpful blogpost from jackmeme. http://www.jackmeme.com/blog.php?id=1

I used it as inspiration and did something similar in Perl. Of course the Imagemagick perl-magick reference was invaluable. http://www.imagemagick.org/script/perl-magick.php

The first thing I thought of with automated meme generation? The Weather! See my previous post about getting data from the Weather Underground API using Perl

This naturally lead to the forecast overlaid on the current radar.
68127-radar

Inspired by http://thefuckingweather.com/, I decided I want my daily forecast in meme form. The first weather-related meme I think of is Ollie Williams.

OllieForecast

But that doesn’t really fit with the meme. Change fucking to motherfucking and Samuel L Jackson is PERFECT!

68127-memetemp

Smoosh those memes together and I’ve got my morning weather!

68127

I found another source of amusement while playing with generating image macros. There is a Pyborg bot in one of the IRC channels I frequent. “She” says some pretty funny stuff, so we keep a quote list. Of course this is ripe for meme-ifying.

meme25

More Evequotes are available in the Imgur Album

Keep reading to see the secret sauce.

Anyway, here’s where the magic happens. In the example, I’m using this as the source meme.

AINTNOBODYGOTTIME

And this is the resulting meme.

meme

[perl]
use Image::Magick;
use warnings;
use strict;

###### Configuration ######
my $fontpath = ‘/home/raging/memes/font/impact.ttf’;

###### Programs Go Here ######

my $line1 = "I GOT BRONCHITIS"; ## Top line of meme
my $line2 = "AIN’T NOBODY GOT TIME FO DAT"; ## Bottom line of meme
my $template = "/home/raging/memes/source/AINTNOBODYGOTTIME.jpg"; ## Uncaptioned meme file
my $outputfile = "/home/raging/memes/meme.jpg"; ## where to save the finished picture with meme overlayed

my $memefile = buildMeme($line1, $line2, $template, $outputfile );
print "Meme Created:t" . $memefile . "n";

###### Danger, Thar Be Dragons ######

sub buildMeme {
my $toptext = shift;
my $bottomtext = shift;
my $memetemplate = shift;
my $memesavepath = shift;

## Read in uncaptioned meme
my $memeimage = Image::Magick->new;
$memeimage->Read( "$memetemplate" );

## get width of uncaptioned meme file and set caption width to 90% of width
my $memeimagewidth = $memeimage->Get(‘width’);
my $maxwidth = int($memeimagewidth*0.9);

## create new imagemagick object for the top line of text
my $toptextimage = Image::Magick->new(size=>’3100×3100′);
$toptextimage->ReadImage(‘xc:transparent’);
$toptextimage->Set(background=>’transparent’);

## create the outline for the top line in huge text
$toptextimage->Annotate(
text => $toptext,
x => 21,
y => 101,
font => $fontpath,
pointsize => 100,
stroke => ‘#000000’,
strokewidth => 4,
antialias => ‘true’,
fill => ‘#000000’);

## create the white fill for the top line in huge text
$toptextimage->Annotate(
text => "$toptext",
x => 20,
y => 100,
font => $fontpath,
pointsize => 100,
antialias => ‘true’,
fill => ‘#FFFFFF’
);

## remove extra empty space from around text
$toptextimage->Trim();

## resize top line text image to 90% of meme width
my $geom = $maxwidth . "x";
$toptextimage->Resize(
geometry=> $geom,
filter=> ‘catrom’,
blur=> 0.9
);

## repeat for bottom line of text

my $bottomtextimage = Image::Magick->new(size=>’3100×3100′);
$bottomtextimage->ReadImage(‘xc:transparent’);
$bottomtextimage->Set(background=>’transparent’);

$bottomtextimage->Annotate(
text => $bottomtext,
x => 21,
y => 101,
font => $fontpath,
pointsize => 100,
stroke => ‘#000000’,
strokewidth => 4,
antialias => ‘true’,
fill => ‘#000000’);

$bottomtextimage->Annotate(
text => "$bottomtext",
x => 20,
y => 100,
font => $fontpath,
pointsize => 100,
antialias => ‘true’,
fill => ‘#FFFFFF’
);

$bottomtextimage->Trim();

$geom = $maxwidth . "x";
$bottomtextimage->Resize(
geometry=> $geom,
filter=> ‘catrom’,
blur=> 0.9
);

## overlay top line of text on uncaptioned meme
$memeimage->Composite(image=>$toptextimage,
compose=>’over’,
y=>5,
gravity=>’North’
);

## overlay bottom line of text on uncaptioned meme
$memeimage->Composite(image=>$bottomtextimage,
compose=>’over’,
y=>5,
gravity=>’South’
);

## write out captioned meme file
$memeimage->Write(filename=>$memesavepath, compression=>’None’);
return($memesavepath);
}
[/perl]

2 thoughts on “Using Perl and ImageMagick to Generate Meme Image Macros

  1. Thanks for posting this. Do you have a redistribution license for this code? Or, no, just posted for informational value?

    Like

    1. This was totally for funzies!
      I linked to resources I used as inspiration. I don’t think there is anything here that requires license for my redistribution. I didn’t think my contribution was worth attaching a license to. If you want to use it, feel free. You can share a link here if you do something cool with it, if you want!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: