Project, Python, Research

Automating Making Memes & Instagram Posts 9: Revisiting The Cat Geek Posts

So I like my inspirational quotes posts, but I would really like them to be memes. I already found the python code necessary to make a meme, but I needed add a few additional lines to split the quotes evenly in half . That way the meme has text that is balance between the top and bottom.

#create meme caption
f = open('/home/pi/etc/lolquotes.txt')
caption = f.readline()
words = caption.split()
upper_meme_text = ""
lower_meme_text = ""
for n in range(1,len(words)+1):
    if n <= round(len(words))/2:
        upper_meme_text = upper_meme_text + " " + words[n-1]
        lower_meme_text = lower_meme_text + " " + words[n-1]
Remember when this all used to be about David Rose from Schitts Creek? Yeah, those were the good ol’ days

After getting everything set to run, my Instagram posts ended up failing because the jpg photo itself was somehow problematic. I started going through the code and found that the resize command was causing an issue.

After commenting it out, I then had to tweak the margins and font size to get it to look normal. Since this was a pain to figure out, I made notes below on key parts of the program for future reference.

I actually don’t know how to survive without taking notes

Controlling text wrap (make smaller if increasing font size, make bigger if decreasing font size): I set mine to 10 (for font size 200)

Controlling font size: I set mine at 200 (for text wrap 10)

Controlling the bottom margin: I changed mine to 200 (for font size 200)

Controlling upper margin (increase if you increase font size): I ended up setting mine to 200 (for font size 200)

Now that I’ve got the kinks worked out of this, I can go ahead and copy this and tweak it to use for my David Rose Bot Instagram. I just had to set up a new folder for David Rose pictures, and then update the filepath variables for the scripts and it was all set to go.

David Rose Bot is finally alive!

After letting the accounts run themselves for a few days, I noticed a new issue: the margins, font size, and text wrapping of my memes all change depending on the height and width of the picture.

In the original meme poster code, the programmer resized the picture so it would work with static values. Unfortunately, if I leave the resize code on, it corrupts the image and it won’t upload to Instagram.

I don’t have time for your BS, Instagram

So now I need to do some math and figure out an appropriate font size to go with the length of the caption text and pixel width of the picture.

Who doesn’t love a good math problem?

Unfortunately, I sat there with a pen and paper and eventually gave up and decided to just restore the defaults sizes and use PIL to resize the photos to a width of 1024. I did change the text wrap number to 25 though.

sudo pip install python-resize-image

#Added to Script:
from PIL import Image
from resizeimage import resizeimage

fd_img = open(meme_photo, 'r')
img =
img = resizeimage.resize_width(img, 1024), img.format)

After a few more days of testing, I decided to make two more tweaks to the script.

First, I rewrote the PHP and python scripts to use filepath variables that already knew where the script was stored. That way I don’t need to deal with managing filepaths and can instead copy and paste the script into any directory I want.

For testing purposes, I needed to be able to freely type php and python into command line to get immediate feedback on the syntax. To do this with php the command “php -a” starts the php interactive mode and “Ctrl+C” ends it. For python the command “python” begins python interactive mode, and quit() ends it.

This will be SUPER useful

After some back and forth, I finally dialed in the PHP code to concatenate the script’s full path location with the different documents in the same folder. I also did a str_replace() function to add in a backslash after a space in the filepath in order to use the exec command to run the phython script.

$dir = dirname(__FILE__);
$photoFilename = $dir.'/photo.jpg';
$file = $dir.'/lolquotes.txt';
$usedfile = $dir.'/usedlolquotes.txt';
$hashtags = $dir.'/hashtags.txt';
$pythonscript = str_replace(" ","\ ",$dir).'/';

/// PYTHON SCRIPT TO MAKE MEME ////////////////////////

Again after a few minutes of consulting Goolge, I was able to make a more flexible python script that referenced its own location as variables.

import sys, os
pathname = os.path.dirname(sys.argv[0])
project_name = os.path.basename(os.path.dirname(os.path.realpath(__file__)))
main_photo = pathname + '/photo.jpg'
directory = '/media/etc/Nikon Pics/' + project_name

The second change I made was to the PHP script. I wanted it to keep retrying to upload a photo if it returned a server error. Since that could potentially cause an infinite loop, I put in a 60 second time limit to automatically end the script.

//keep trying upload until success
$failupload = true;
try {
    $photo = new \InstagramAPI\Media\Photo\InstagramPhoto($photoFilename);
    $ig->timeline->uploadPhoto($photo->getFile(), ['caption' => $captionText]);
    $failupload = false;
} catch (\Exception $e) {
    echo 'Something went wrong: '.$e->getMessage()."\n";
    $failupload = true;

Now that I’ve changed the resize image code, put in a loop to keep trying when encountering server errors, and made the script more flexible I’m finally in a place to move on to my next project! This new project is a very personal one to me, and I’m going to be utilizing both the neural network and automatic Instagram posts code. So until then, here are some more cat pics! 🙂

Leave a Reply