Lim Yoong Kang

txtimg -- A library for text-based images

Recently, I’ve been doing some translation work for a certain community on the internet. As far as I’m aware of, the community mostly lives on Twitter, and a few other niche forums.

Anyway, sometimes the translations can get pretty long, e.g. a radio or a TV broadcast.

That means, a tweetstorm would be really hard to read.

The solution people usually come up with is to write the translations in a word processor, and take screenshots of the file. So the result is something we call a “text-based image”.

I don’t really use a word processor much, and I hate taking screenshots like that. My tool of choice is a text editor like VSCode, but that looks really terrible when you take a screenshot.

Also, I’ve been toying with the idea of distributing my translations via a web service. Some translations I do aren’t of publicly available content, so the idea is to deliver the translations to only people who have paid for this exclusive content. With a web service, I could also invite other translators to the provide translations.

So I wrote some Python code! I released a library called txtimg!

You can install it like this:

$ pip install txtimg

And here’s how you use it:

from txtimg import TxtImg

text = """
What did Sushi A say to Sushi B?

What's up B? (WASABI)
"""

t = TxtImg()
img = t.generate_from_text(text)
img.save("wasabi.png")

This is basically a thin wrapper on top of the Pillow library. Basically you can pass in a string, and it will return a Pillow image object. There are some configuration parameters that I probably should have put more thought into, but it works.

In hindsight, I could have also just used pandoc but that might be complicated to install on a server. Or it was probably just LaTeX that was difficult to install, I can’t remember.

I’m planning to add more features to this depending on how it goes with my translating thing.

If you want to see my code repo, you can find it here: https://github.com/yoongkang/txtimg