0

I have been using Python 3 and Pillow in Jupyter Notebook to take a string that was input by the user and turn it into an image.

Once the string has been successfully changed to an image, the font size and style of the image remains tiny and won't change when I try to edit the variable associated with the text width and height, but the bordering space does change.

For example, if I enlarge the text width or height, the font does not change but the white boarder around the font will change. Please let me know if anyone knows how to fix this.

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Verdana.ttf", 100) #seems to have no impact on the program

userinput0000 = input("Enter String Here:")
bgcolor = 'white'
txtcolor = 'black'
text_width, text_height = (5 , 5) # (w,h) changes the size of the white box surrounding the string

userinput0000img = Image.new("RGB",(text_width, text_height), bgcolor)
draw = ImageDraw.Draw(userinput0000img)
draw.text((5, 5), userinput0000, txtcolor) #(x,y)changes the xy coordinates of the string

userinput0000img.show()

im = Image.open("Desktop/0001.jpg")
x, y = userinput0000img.size
im.paste(userinput0000img,(0,0,x,y))
im.show()

Current code output:

These are two Stack Overflow posts that I found to be useful but neither seemed to be addressing the same type of problem.

I have also read through the Pillow handbook (linked bellow) and found no reference to a similar situation.

https://pillow.readthedocs.io/en/latest/handbook/tutorial.html

1 Answer 1

1

You missed to add the font parameter in your ImageDraw.text call, thus referencing to the font instance you created.

So, change

draw.text((5, 5), userinput0000, txtcolor) 

to something like

draw.text((5, 5), userinput0000, txtcolor, font=font) 

and you're done.

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that fixed the problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.