Creating Twitter Cards dynamically with Python

So, I'm on Hashnode and I want to expose my articles even more. There is a field within the articles settings on Hashnode where you can put a twitter card image. I really never used twitter cards, I just hoped the Cover photo would be enough to show what the article is about on twitter.

But being on Hashnode it has a great community with a lot of great articles. So browsing some articles in my feed I saw this article written by Braydon Coyer about creating twitter cards dynamically.

When reading his great post, I could not stop thinking.... this can be done with Python. And, sorry to say so Braydon, also much easier and cheaper.

Create a Twitter card with Python

There are some things you need before you can actually start creating twitter cards with Python.

First you need a pre-made PNG image that you want to use as basis for you cards.

Part of the PNG image needs to be transparent and prefilled with some info, like your name, your photo and site name or url.

Something like this:

twitter-card.png

The size of the image should be 1200 pixels by 628 Pixels.

You need the name of the font you are using (it has to be installed on your machine).

For this script to work you need to install Pillow and textwrap3.

What is Pillow

PILLOW is a Python Imaging Library fork for cross-platform image processing. It is designed to have a simple interface with no external dependencies, so it's easy to use and ready to go when you are.

PILLOW provides an easy-to-use interface with no external dependencies, so you can start using it immediately.

I'm using PILLOW within this script to put text on a PNG file and to use another image as background for the transparent part of your twitter card.

You can install Pillow by: pip install Pillow

What is textwrap3

Textwrap3 is a Python library for text wrapping and filling. It can be used to wrap text at a specified line length or leave the text as one long line.

It is important to note that Textwrap 3 is an open source library, so its available free of cost. This makes it the perfect tool for beginners who might not have access to expensive software or just need a simple solution.

Textwrap3 is being used to wrap the text when its to long and goes over the transparent part. You will have to play around with the value to get it right on your twitter card template.

You can install textwrap3 by: pip install textwrap3

Other needs for this script

You need the following folders

  • background
  • png

In the PNG folder you can put your twitter card template. The file should be a transparent PNG. Naming it "twitter-card.png" make things easier.

In Background you can put the image file that comes behind your twitter card template.

You also have to make your own adjustments to this script. As Text placement, coloring and image names.

The only things you need for the first time use are the below items.

text = "Creating Twitter Cards dynamically with Python"
background_image = "backgrounds/background_img.jpg" 
foreground_image = "png/twitter-card.png" 
font = "AllertaStencil-Regular.ttf"
font_size = 55
text_wrap_width = 22 # width when text is wrapped
offset = 44 # pixels from the left
margin = 186 # pixels from the top
color = (249,249,249)

Each time you want to create a new twitter card you need to change the text value and the background_image value.

And that's it.

When you filled the fields and do a python twitter-card.py the image will be created for you.

creating_twitter_cards_dynamically_with_python.jpg

Python code for dynamic Twitter Card

Just copy the following code and past it in a empty file naming it twitter-card.py.

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import textwrap

text = "Creating Twitter Cards dynamically with Python"
background_image = "backgrounds/brett-jordan.jpg" # this is the background
foreground_image = "png/twitter-card.png" # this is your default card
font = "AllertaStencil-Regular.ttf"
font_size = 55
text_wrap_width = 22
offset = 44 #pixels from the left
margin = 186 #pixels from the top
color = (249,249,249)

## DO NOT change below this line!!
save_name = f"{text.lower().replace(' ', '_')}.jpg"
textwrapped = textwrap.wrap(text, width=text_wrap_width)

foreground = Image.open(foreground_image)
background_width, background_height = foreground.size
foreground.convert('RGBA') # To make sure the foreground contains transparency in all cases

background = Image.open(background_image)
background.convert('RGBA')
background = background.resize((background_width, background_height))
background.paste(foreground, (0, 0), foreground)

draw = ImageDraw.Draw(background)
font = ImageFont.truetype(font, font_size)
draw.text((offset,margin), '\n'.join(textwrapped), font=font, fill=color)

background.save(f"output/{save_name}")

Did you find this article valuable?

Support itheo.tech, I automate Sh!t by becoming a sponsor. Any amount is appreciated!