0

I have figured out how to add text to an Image and this is how I have implemented it.

import docx
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

img = Image.open("a.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(<font-file>, <font-size>)
font = ImageFont.truetype("arial.ttf", 16)
draw.text((x, y),"Sample Text",(r,g,b))
draw.text((0, 0),"Sample Text",(255,255,255),font=font)
img.save('C:\\Users\\John\\Desktop\\See\\sample-out.jpg')

From the documentation (draw.text()) i understand it takes position (xy) as the first argument.

How can I pass 3 variables (firstplate, secondplate, thirdplate) to the text argument and have them dispalyed randomly diagonally (like red) across the Image like below?

Am having great difficulty figuring out how to make the text diagonal, and add them in a loop all across the image.

The Goal

Johnn Kaita
  • 432
  • 1
  • 3
  • 13
  • 1
    What have you tried? Where are you stuck? It seems like you are on the right track, you just need to define a function that takes in the 3 (or list of) strings. – jtbandes Dec 16 '20 at 16:51
  • @jtbandes, am mainly stuck trying to rotate the text to a diagonal position, I have a little edit to the question – Johnn Kaita Dec 16 '20 at 16:59
  • You have this answer to rotate the text : https://stackoverflow.com/a/245892/10409093 . You can create three rotated images with text, then make a loop and use `random.choice` to pick randomly one of the three images to paste. – Whole Brain Dec 16 '20 at 17:02
  • 1
    Thanks, am trying jtbandes and Whole Brain's guide. Will update the question with the answer once I have it working – Johnn Kaita Dec 16 '20 at 17:07

0 Answers0