Generate funny memes programmatically

ยท

4 min read

You see hundreds of memes daily and many of them are really very funny. You can create memes from any app, but do you know that you can create memes programmatically too? Yes and TBH it's very easy! ๐Ÿ˜‹

I know your expressions after listening this: ๐Ÿ˜‚

SAVE_20220515_224554.jpg

Now, in this post, you will learn that how you can create funny memes programmatically and for this, I'll use PixLab API. Let's make it!

Create memes programmatically

images (8).jpeg

Memes have become a huge part of today's social life. It came into popularity in 1996 when the Dancing Baby or Baby Cha-Cha meme went viral on the internet. Today, memes are also being created through some digital technologies such as computer vision and machine learning.

Computer vision uses Artificial Intelligence (AI) to train modern computers to understand visual world. I'll not bore you by explaining something out of topic, so let's know about the API through which we can generate memes in seconds.

PixLab API

Talking about Pixlab, it is a machine learning SaaS platform which offers Machine Vision & media processing APIs for developers via Web or Offline SDKs.

It is shipped with over 130 commands (API endpoints) including:

  1. Face detection, recognition, emotion, generation, lookup, landmarks.
  2. Content Moderation & Extraction: nsfw, sfw, urlcapture, header, ocr, tagimg.
  3. Pixel Generation , including human faces and landscapes.
  4. Image processing : blur, composite, negate, grayscale, oilpaint, etc.
  5. The ability to train your own object detector .
  6. Media encryption & decryption .

All these are available for you at GitHub code sample page and are waiting for you to make something awesome! ๐Ÿ˜Ž

So let's talk about the API!

Here, we'll programmatically generate a funny meme by writing some funny texts over this image:

jdr.jpgThis is the input image

jdr_draw.jpgAbove is the programmatically generated meme

The above result is obtained via the following python gist.


import requests
import json
# Draw some funny text on TOP & BOTTOM of the famous Cool Cat public domain image.
# https://pixlab.io/cmd?id=drawtext is the target endpoint for drawing text on images
req = requests.get('https://api.pixlab.io/drawtext',params={
    'img': 'https://pixlab.io/images/jdr.jpg',
    'top': 'someone bumps the table',
    'bottom':'right before you win',
    'cap':True, # Capitalize text,
    'strokecolor': 'black',
    'key':'PIXLAB_API_KEY', # Get yours from https://pixlab.io/dashboard
})
reply = req.json()
if reply['status'] != 200:
    print (reply['error'])
else:
    print ("Meme: "+ reply['ssl_link'])

Python gist source code: github.com/symisc/pixlab/blob/master/python..

The same logic using PHP:


<?php
/*
 * PixLab PHP Client which is just a single class PHP file without any dependency that you can get from Github
 * https://github.com/symisc/pixlab-php 
 */
require_once "pixlab.php";

# Draw some funny text on TOP & BOTTOM of the famous cool cat.
# https://pixlab.io/cmd?id=drawtext is the target endpoint for drawing text

# Cool cat image
$img = 'https://pixlab.io/images/jdr.jpg';

# Your PixLab API Key
$key = 'PIXLAB_API_KEY'; # Get yours from https://pixlab.io/dashboard

/* Process */
$pix = new Pixlab($key);
if( !$pix->get('drawtext',array(
            'img' => $img,
            'top': 'someone bumps the table',
            'bottom':'right before you win',
            'cap' => true, # Capitalize text,
            'strokecolor' => 'black'
        )) ){
    echo $pix->get_error_message()."\n";
    die;
}
echo "Pic Link: ".$pix->json->ssl_link."\n";

?>

Php source code: github.com/symisc/pixlab/blob/master/PHP/ma..

Here, these terms refers to:

  1. top - the part of text which will lie on the top of the image.
  2. bottom - the part of text which will lie on the bottom of the image.
  3. cap:true - this specifies that the text will be in capital case.
  4. strokecolor - stroke is what the outline of text is colored with.

DRAWTEXT is the sole endpoint needed to generate such image. It expect the text to be displayed on TOP, CENTER or BOTTOM of the target image. This endpoint is so flexible that you can supply Font Name, Size & Color, Stroke width, etc. Refer to DRAWTEXT documentation for additional information. Notice that there is a more flexible endpoint named DRAWTEXTAT which let you draw text on any region of the target image by specifying the X & Y coordinates instead.

Conclusion

As we saw, generating memes programmatically is too much simple even for the beginners who have started recently, thanks to the open computer vision technologies provided by PixLab.

There are more APIs for you on PixLab that you must check out!!

  1. Guess human emotions.
  2. Blurring human face.
  3. Encrypting images and many more!

Find out more such code samples on PixLab GitHub page.

ย