Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

python - Draw a rectangle and a text in it using PIL

I want to draw a rectangle and a text in it, here's a part of my code and it's a bit obfuscated:

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageEnhance

  source_img = Image.open(file_name).convert("RGB")

  img1 = Image.new("RGBA", img.size, (0,0,0,0))
  draw1 = ImageDraw.Draw(watermark, "RGBA")
  draw1.rectangle(((0, 00), (100, 100)), fill="black")
  img_rectangle = Image.composite(img1, source_img, img1)

  draw2 = ImageDraw.Draw(img1, "RGBA")
  draw2.text((20, 70), "something123", font=ImageFont.truetype("font_path123"))

  Image.composite(img1, source_img, img1).save(out_file, "JPEG")

This draws them both, but they're separate: the text is under the rectangle. Whereas I want a text to be drawn inside the rectangle. How can I do that? Should I necessarily compose them or what?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it without composite()

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

source_img = Image.open(file_name).convert("RGBA")

draw = ImageDraw.Draw(source_img)
draw.rectangle(((0, 00), (100, 100)), fill="black")
draw.text((20, 70), "something123", font=ImageFont.truetype("font_path123"))

source_img.save(out_file, "JPEG")

You can create empty image with size of button and put text on it and later put this image on source_img. This way long text will be cut to size of button.

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

source_img = Image.open("source.jpg").convert("RGBA")

# create image with size (100,100) and black background
button_img = Image.new('RGBA', (100,100), "black")

# put text on image
button_draw = ImageDraw.Draw(button_img)
button_draw.text((20, 70), "very loooooooooooooooooong text", font=ImageFont.truetype("arial"))

# put button on source image in position (0, 0)
source_img.paste(button_img, (0, 0))

# save in new file
source_img.save("output.jpg", "JPEG")

EDIT: I use ImageFont.getsize(text) to get text size and create button with correct size.

from PIL import Image, ImageFont, ImageDraw, ImageEnhance

source_img = Image.open("input.jpg").convert("RGBA")


font = ImageFont.truetype("arial")

text = "very loooooooooooooooooong text"

# get text size
text_size = font.getsize(text)

# set button size + 10px margins
button_size = (text_size[0]+20, text_size[1]+20)

# create image with correct size and black background
button_img = Image.new('RGBA', button_size, "black")

# put text on button with 10px margins
button_draw = ImageDraw.Draw(button_img)
button_draw.text((10, 10), text, font=font)

# put button on source image in position (0, 0)
source_img.paste(button_img, (0, 0))

# save in new file
source_img.save("output.jpg", "JPEG")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...