Activity 10: Making a meme!
One other thing we can do to enhance our image is to add 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. 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.
drawnImage = ImageDraw.Draw(img) drawnImage.text((200,20), "When you realize you learned python in an hour.") drawnImage.save("myCatWithText.jpg")
My image now looks like this:
Challenge - Change the font
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.