Inspirational Quotes Image Generator in Python
2 min read • June 12, 2023
If you browse Pinterest, Instagram, or type “inspirational quotes Pinterest” in your browser, you’ll get tons of results, like these:
I decided to automate the process of creating these images with Python using the Pillow library.
It’s also my Harvard’s CS50P course final project (check out my CS50P review).
How does it work? 🤔
Short video demo:
and a graphic diagram of the process:
Main functions explained
I used the Pexels API to fetch images for the background. We can use the
download_background()
andget_backgrounds()
functions to download one random picture with a matching query. I decided to go with “nature”.After getting the background image, we need to edit it. We will resize the background and darken it using
edit_background()
to prevent the image and text from blending together.Now it’s time to get the quote and its author. They’re stored in a CSV file (
assets/quotes.csv
). We just have to get a random one with theget_quote()
function.The last step is to insert the quote using the Pillow library. The toughest part is fitting the text onto the image. Quotes have different lengths, so we have to change the font size based on its length. I achieved it with the help of helper functions inside the
ImageText
class (img_utils.py
file).
The entire source code is available here on GitHub if you want to check it out. 😀
Donate
This site and articles are 100% created and maintained by me. If you found it useful and would like to support my work, please consider fueling my coffee addiction. ☕
Thank you! 💖