Making a meme!

Workshop Resources

So now that my image looks how I want it to look, I want to add some text. We can do that by utilizing the Pillow ImageDraw module. Let’s import that at the top of our file along with our other import statements.

    from PIL import Image
    from PIL import ImageFilter
    from PIL import ImageDraw

To allow us to add text, we must redraw the same image we used earlier for image manipulation. Let’s do that using the draw() function, which takes in an image object. Once the image is drawn, we can then add text to it by using the text() method to set the text of the image. The text is drawn based on coordinates provided. The function takes in two mandatory parameters: the xy coordinates and the text needed.

For example:

    drawnImage = ImageDraw.Draw(greyscaleImage)
    drawnImage.text((200,20), "When you realize you learned python in an hour.")"myCatWithText.jpg")

My image now looks like this: alt text


You can see that the text in the image created above is in a small, default font. There are other parameters within the text() method you can use. Take a look at the documentation and see if you can change the font and the font size, as well as the color of the text!

This will require downloading a .ttf (aka a font file) from online! Ask for help if you have any questions.