DEV Community

Cover image for Create your own memes in Terminal
Eric F 🇺🇦
Eric F 🇺🇦

Posted on

Create your own memes in Terminal

To create a “meme”, or just adding text to an image - you don't need an app or an online service. You can do it yourself, in Terminal, using ImageMagick. Here's a short snippet you can use.

Since the command is kind of long, you can save in some file, or make a bash function - or maybe a script.

# ImageMagick v6
$ convert in.jpg -gravity south -font Impact -pointsize 120 -fill white -stroke black -strokewidth 3 -annotate +0+0 'Add text here' out.jpg

# ImageMagick v7
$ magick in.jpg -gravity south -font Impact -pointsize 120 -fill white -stroke black -strokewidth 3 -annotate +0+0 'Add text here' out.jpg
Enter fullscreen mode Exit fullscreen mode

 

Text position

For -gravity you can use your normal: north, south, center, southeast, etc.

 

Fonts

I used Impact in the example. It's usally installed. But, if you want to change the font - just use another one from the list of fonts. Most font names are formatted with a dash, like “DevaVu-Sans-Bold”, “NimbusSans-Bold”, etc.

If you try one and it returns an error - you can get the list of fonts (ie. the font names) using:

# ImageMagick v6
$ identify -list font

# ImageMagick v7
$ magick -list font
Enter fullscreen mode Exit fullscreen mode

To get a more readable list, you can use:

$ identify -list font | grep 'Font'
Enter fullscreen mode Exit fullscreen mode

 

Text stroke

The size of the text stroke depends a little on the font, and what you like. 1-3 are good values. I usually go with 2, and sometimes (depending on the font) I use 1 or 3.

3 is probably closest to what you get at an online meme generator.

 

Example

Here's an example adding text 2 times (top and bottom).

$ magick in.jpg \
  -gravity north -font Impact \
  -pointsize 120 -fill white  \
  -stroke black -strokewidth 3 \
  -annotate +0+0 'Text at top' \
  out1.jpg
$ magick out1.jpg \
  -gravity south -font Impact \
  -pointsize 120 -fill white \
  -stroke black -strokewidth 3 \
  -annotate +0+0 'Text at bottom' \
  out2.jpg
Enter fullscreen mode Exit fullscreen mode

out2.jpg


It's more fun to spend time in Terminal, than at some online meme generator page. Plus, if you dig deeper into ImageMagick - you can add a lot of other effects as well. This was just a simple example.

 

// Happy hacking… 👍

· Eric

Top comments (2)

Collapse
 
chrisgreening profile image
Chris Greening

Lol this is a really fun tutorial, thank you for sharing Eric!

Always love learning new ways to use command line tools, I've had a lot of fun building all sorts of little DIY projects with ImageMagick, FFmpeg, etc

Collapse
 
efdev profile image
Eric F 🇺🇦

Thanks Chris! 👍

Yes, you can do a lot of DIY witht those to. I use ffmpeg as well, to create mp3's and m4a's. So much easier when you have them in like a function or script - instead of using a GUI program. :)

/* Beside ImageMagick, to optimize the images - I also use jpegoptim, optipng and gifsicle. They're also really easy to use with scripts/functions. :) */