Hey everybody, it’s Trafotin, the same Trafotin from Huffington Post article no one read! I must have depression or something and no joke, I was staring at my Mastodon and TweetDeck thinking there has to be a way to make my posts better! It’s so bland with just text and I need something to make it better! Sure, you could make the argument that it’s much more bandwidth efficient and lightweight, but images are what grab everyone’s attention! And as a YouTuber, I need to get my hands on that sweet sweet attention juice because without it I’ll die!

I started researching someone who had a quick answer to my problem and you’ll get plenty I covered before: Canva, Adobe Express, and Microsoft Designer. But I just want a simple prefabricated post background and I don’t want to interact with all of these subscription, always online (watching and listening) services to do this!

But turns out the answer I was looking for was one of such evil online services. Yes, more evil than Adobe! More evil than Microsoft–Facebook.

Facebook lets you add a background to your post, but what they are really doing is taking the text of your post and overlaying it on top of an image. And when I saw that, that’s when I knew this is what I wanted. The problem is using such a feature requires a Facebook account, where you use your real name(not your native or “chosen” name), an email, a (non-VOIP/Google Voice) phone number, and a picture of you holding your passport or driver’s license because “the future is private.”

I know that we don’t exactly have the strongest reputation on privacy right now, to put it lightly.

Mark Zuckerburg, Facebook F8 2019 Day 1 Keynote | 5:13

I decided to accomplish this by writing this as a shell script. There’s no need for a dedicated program or low level languages here, so why reinvent the wheel? The game plan is this:

  • have a library of stock images or preset media
  • Feed image into sdtin for ImageMagick.
  • use ImageMagick to generate the specified text I want
  • Overlay text on top of the stock image

Convert Image to Specification

First, social media platforms have image specifications. This is where it’s important we convert our image to get the aspect ratio correct. Many social media platforms often specify your images are at least 1080p and preferably square, but why? On mobile devices, which is where most users actually view images, images that are 16:9 or 19:10 are acceptable, but they get cut off because of the screen sizes of phones. As a result, post photos on social media uses a 1:1 aspect ratio.

(For example, movies like the Dark Knight have its action scenes shot in IMAX, which closer fits 16:9, but TV shows like Homecoming have its flashback scenes shot in 1:1.)

So let’s break down some of what we can do with ImageMagick. ImageMagick is a command-line tool for manipulating images. Now before you think you’ve never used ImageMagick before, odds are you have in a different way. If you use or visited a WordPress website (which is 25% of the internet btw), WordPress features integrations for resizing its images with ImageMagick.

Note for Windows users, you need to append magick.exe to the beginning of all of these commands and make sure ImageMagick is in your Powershell path. You could also use WSL.

I downloaded the Deepin wallpapers, which they use photos from Unsplash, a royalty free image sharing site. I also wrote a command to size down each photo to meet the 1:1 aspect ratio.

In ImageMagick, this is a simple “resize” command, then cropping off the edges:

convert "$1" -gravity center -crop 2000x2000:0:0 -resize 1080x1080 fbbkg_background.jpg

This preps the user’s image to meet those 1080x1080 standards, cropping the sides, and maintains the center of the image. I’ll also admit this is fulfill my needs. This script will look weird if you use an image smaller than 1080p, but you shouldn’t be uploading blurry photos to social media…

Generating Text in ImageMagick

Next, we’re going to be generating some text. While ImageMagick has many ways to generate text, many of them often do not support text wrapping, because if you don’t the text is going to trail off the image.

convert -background transparent -font Source-Sans-3-Bold -size 490x480 -fill white -strokewidth 2 -stroke black caption:"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magnam aliquam quaerat voluptatem."
  • -background transparent The text needs a transparent background before it’s layered on the background image.
  • -gravity center This ensures the text is centered in the image.
  • -font Source-Sans-3-Bold I use Adobe Source Sans 3 when I do hard subtitles in my YouTube Shorts, but you can pick a different font using identify -list font then change this to whatever font you want, under “Font: your font.”
  • -size 480x480 This is the size of the image with the generated text. Ideally, it needs to be smaller than 1080x1080 and sit right in the center of the image.
  • -fill white The text itself will be white as white is more visible in most scenarios. ImageMagick has lots of different preset colors and you will need to consult their documentation’s list.
  • -strokewidth 2 The text itself will be given a 2 px outline.
  • -stroke black The text outline is black #000000.
  • caption: your caption The text we’re putting into the image.
  • at the end at the name of your image. I’m using PNG as the file extension, but you could also use WEBP. This won’t work on image formats that don’t support transparency like JPG and GIF.

Overlaying the Images

Now we have 2 images: the text and the background. Let’s overlay them using the composite command.

fbbkg_name="$1"
composite -gravity center text_fbbkg.png "$1" "${fbbkg_name%.*}-fb.jpg"