Auto-generating social media posters using Golang

My SLTP Walkthrough: Part 5

XQ
The Research Nest
7 min readSep 26, 2020

--

Photo by Rodion Kutsaev on Unsplash

There are a bunch of interesting things we are going to explore here. This article can be considered as a stand-alone in those aspects. However, if you want more context, you can read the previous part of my SLTP series (which I highly recommend if you have some time).

The objective here is to programmatically create a social media poster using Golang. I am specifically looking to build it using the news articles we have fetched from the RSS feed in part 4. I will try to create a minimum viable function and then polish it to make it more structured and compact in future articles. With that being said, keep in mind that the approach I use can be heavily improvised.

What I already have in my code

  • The fetchNews(url string) function- This takes the URL of the RSS feed as input and returns the required news data to us.

Using this news data I want to make the poster. Logically, this can be broken down into several steps-

  • Create an empty image with the required dimensions.
  • Fill the image with an appropriate background.
  • Have an overlay over the background where we can place the title and main content.
  • Some attribution (@the_research_nest) at the corner of the poster.

These are some typical elements in a content poster. I haven’t finalized the exact design template but for now, this should be enough as a proof of concept.

The function I want to create must take in the output data we get from the fetchNews function and return us the poster we want.

At first glance, this looks like a very open-ended problem. Where do I even start? I literally googled the title of this article to find my answers.

One of the first links you get is this-

I went through the above article and it pretty much is exactly what we want. Thanks to Mat Ryer for this amazing tutorial. Now, it is time for some experimentation (and OBL- observation-based learning) to check what works and how it works.

I followed the steps in the above tutorial but it did not run as expected. There were glitches in the code. I was changing some lines on my own, completely by trial and error method to resolve them.

After tinkering with the code for a while, I got it working. Then I modified it as per my requirements. Here are all the steps I followed-

  • First, I downloaded a background stock image for my poster from Pexels. Also, I decided to have my poster dimensions as 1500*1500.
Stock background image
  • Next, I went through the documentation of gg library that was used in the above tutorial to get some basic context of it. (Huge shoutout to Michael Fogleman for creating this amazing Go library!)
  • I downloaded the Open-Sans font from google fonts as per requirement, extracted it into my project folder, and used this path to get the font style.
  • Then, I simply tried to replicate the results from the tutorial with my input values and background image. As I mentioned, it threw a bunch of errors. I removed all the unnecessary code and tried to segregate it into reusable functions.
  • To summarise, I created three functions, drawOverlay, drawTextand drawPoster. Below is the code for each of them.
  • Carefully observe these functions and the code samples I have referred to understand the changes.
  • When you read the code, you will notice that it is essentially mathematics and geometry. I haven't changed much here from the tutorial reference used.
  • createPoster is the main function which when called will take the input data, create the image, and save it as a PNG file. For now, I have planned to make a poster with the list of all the latest news headlines which are accessed from data[n].Title.
  • There was one issue with these headlines- some of them had HTML tags in between. I used the strip.StripTags to remove the same, which I found by a simple google search.
  • Another issue I faced was in knowing the type of element which was required in creating the functions. For example, in drawOverlay you can see the dc *gg.Context piece of code. Here, dc (variable name) is the expected input and its datatype was *gg.Context. This is the syntax followed in Go. To know the same for some variables I used the reflect.TypeOf function.

It is time to finally call these functions in our main method. Here’s the code snippet for the same-

  • First I call the fetchNews() function and store the result in a temp variable.
  • I pass this variable to the createPoster() function.
  • Notice the Println statements I left for your reference. Whenever I want to understand what data the variables store or other aspects like that, I simply print them as such. This gives a better picture of the entire flow.
  • Running the program should create and save a poster image containing the latest news headlines related to AI and just as expected, here is the output I got-

Viola! All we need to do now is design a better template, add more customizations like fonts, images, etc. and finalize the exact content we want to display on the poster. Notice that the entire process is dynamic. We are not hard coding this news data.

How did I figure out all this? I want to answer this for the absolute beginners, the first time programmers. The secret lies in breaking and making the code. Mess your code up and then take the help of Google and StackOverflow to fix it. Try different things. Even if it is a wild guess, that is okay. You will truly learn the flow when you make mistakes and try to resolve them. It is not really difficult. As you can see, understanding your problem statement well, and identifying how to use the code samples already there on the internet in your context is the key. The technique of observation-based learning which I talked about in my previous article will be of immense help in most places.

What next?

At this point, I already started noticing a lot of bugs. I have placed the text in fixed coordinates which can cause some issues and overlapping. All content on the poster must be placed relative to other elements. I will work on it in the next article.

The codebase is quite messy right now. I plan to clean it up and keep it organized. While implementing this, there were several other ideas that popped up in my mind. I had two questions to ask, which are applicable in most scenarios-

  1. How do I make this as modular as possible?
  2. Where else can I use this approach?

One thought I had was like what if I created a separate Go wrapper package on top of the gg library that we used and include all the functions I need inside that. I can then import it and use it for my specific requirements directly without cluttering my main program.

Such a standalone package can also be reused to create other types of posters. Then I realized that there are a few more things I can do-

  • Create content like the fact of the day, the thought of the day, etc.
  • Make my poetry posters automatically.

I generally use Canva to design my posters. For all the regular posters I create in a specific template, I can use this approach to automate that flow.

When you notice these thoughts, you will realize that generating posters in itself is a standalone project in its own right. There are a lot of customizations you can do here. There is also the potential to use advanced AI-based techniques, which I will not explore at the moment to avoid any further complexity. Creating a fully-fledged Canva like software API platform to programmatically generate graphic design posters is a legit startup idea in itself. Such a product can be used by companies to automate their content creation workflow.

In the next article, I will be doing some code cleanup, bug fixes, design a proper template, explore a bit of the geometry and mathematics used, and conclude the building of this feature. #StayTuned

For more details about SLTP 2020, visit-

For the previous article in this series, visit-

Disclaimer- While I try to present the best (or the most convenient way) of what I know at the moment in building products, I want you to be aware that there can be other better ways to do something that I describe here. Always stay vigilant and keeping exploring. With that being said, if you have any suggestions, do let me know.

--

--

XQ
The Research Nest

Exploring tech, life, and careers through content.