In my project I need to load an image an display a text in it, but the text needs to be written after executing the application. I tried using tkinter
but it didn't give me the desired result, so I was wondering, can I add a text in my image using the console of spyder
? I mean I display the image, I write my text in the console, and once I hit enter
The text is shown in my image.
via this link you'll find my attempt in doing it using tkinter
. insert text on image, python
Thank you.
Asked
Active
Viewed 81 times
1

newbie
- 646
- 8
- 27
1 Answers
1
It's actually pretty easy to do, for those who might need it, here is the code to do it:
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
img = Image.open('rect.png')
var = input("Please enter something: ")
print ("you entered", var)
font = ImageFont.truetype("arial.ttf", 20)
draw = ImageDraw.Draw(img)
draw.text((0,0), var, (255,255,0), font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
img.show()

newbie
- 646
- 8
- 27