in Hacks

Inspirational quote Part 1

Every time I see the hostility in the Python movement I get drawn nearer to Clojure. It’s crazy but it’s something that can destroy a community.

Nonetheless, I wrote some code in Python. I was looking around for a project to apply some Clojure. I had some idea but none was overwhelming engaging.

Thankfully, last evening a found a post in /r/RequestABot on which someone requested a bot which should create inspirational quote pictures based on comments. It was an elaborate joke but some intriguing problem.

I was engaged and started writing some code. I just wanted my program to generate inspirational quote pictures based on a random string.

I think it may be interesting how I approached the problem. I’ve written some picture generating software – albeit pretty simple – in the past. So I started off looking at Pillow which is a fork of PIL – the Python Image Library.

My first to problems were:
a) How can I load an image into Python?
b) How can I write text with a specific font into the image?

To test that I downloaded a background image and a font from GoogleFonts. Then I started the REPL. This is something I did even more since I started writing some Clojure. The focus on the REPL in Clojure is pretty big so I just adopted it into Python.

Solving a) was pretty simple. I just read the docs for Pillow – more specifically the tutorial and the first code example reads:

To load an image from a file, use the open() function in the Image module:

Bingo. So I started on the REPL and typed:

It worked. The next step was looking at the second problem. How do I load a font? Again, docs and there was a font module. I simply tried it out and it worked without problems. I don’t go into the details, you can read the code if you like.

Now that these two things worked I wrote down my first code. Basically I copy and pasted my working REPL commands.

I solved problem a) and b) but now new problems appeared:
c) I wanted to get a random background
d) I wanted to get a random font

These problems are very similar. So what I really wanted was:
c+d) I want to get a random file from a directory

Also easy to solve. It worked and I put everything together. Now I had a function which returned a random background and a random font. My composition function was the same and it worked fine.

Part 2 tomorrow

Write a Comment

Comment