简体   繁体   中英

How could I add text to an image using PIL?

I've seen many questions and answers here, but they're not for Python. I'm trying to make a meme generator, and I have a folder full of emme templates. I want to make the user get a meme template, view it then add text. Here is my code so far:

from PIL import Image
im = Image.open("Two-Buttons.jpg")
im.show()

Yeah, I know it's not much but I need help with getting user input on a certain part of the text.

First, you would need to import the necessary objects to draw on images from the PIL library:

from PIL import Image, ImageDraw, ImageFont

Then, you instantiate the ImageDraw class:

draw = ImageDraw.Draw(im)

Then you can simply do the following to draw on the original image im :

text = 'Insert meme here'
font_type = ImageFont.truetype("arial.ttf", 18)  # 18 -> font size
draw.text((100, 100), text, (255, 255, 0), font=font_type)  # "draw.text(LOCATION, TEXT, COLOR, FONT_TYPE)"
im.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM