How to use Data Science Superpowers for Useless Things: Adding Text to Images (aka Cats Narrate the Big Lebowski)

less than 1 minute read

Cats Narrating Big Lebowski


from PIL import Image, ImageDraw, ImageFont
stroke_width=2,
stroke_color=(0, 0, 0),

# img = Image.open('cat2.jpg')
# d1 = ImageDraw.Draw(img)
# myFont = ImageFont.truetype('Josefin_Sans/JosefinSans-VariableFont_wght.ttf', 300)
# d1.text((0, 0), "It's the LeBaron.", font=myFont, fill =(255, 255, 255), stroke_width=1, stroke_fill=(0,0,0))
# img.show()
# img.save("image_text.jpg")

images = ['cat1.jpg', 'cat2.jpg', 'cat3.jpg', 'cat4.jpg', 'cat5.jpg']

text_for_image = [ 'A way out west there was a fella, \n fella I want to tell you about, \n fella by the name of Jeff Lebowski.',
 'At least, that was the handle his lovin parents gave him, but he never had much use for it himself.',
 'This Lebowski, he called himself the Dude.',
 'Now, Dude, thats a name no one would self-apply where I come from.',
 'But then, there was a lot about the Dude that didnt make a whole lot of sense to me.',
 'And a lot about where he lived, like- wise.',
 'But then again, maybe thats why I found the place sdurned innarestin.']

font_path= "Josefin_Sans_Bold/static/JosefinSans-Bold.ttf"
for i,image in enumerate(images):
  img = Image.open(image)
  d1 = ImageDraw.Draw(img)
  myFont = ImageFont.truetype(font_path, 100)
  d1.text((0, 0), text_for_image[i], font=myFont, fill =(255, 255, 255), stroke_width=1, stroke_fill=(0,0,0))
  img.show()
  image_name= "cats_lebo_v2_{}.jpg".format(i)
  img.save(image_name)