-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How does writing text in images with random angles work? #5738
Comments
Does this help? |
Still can't achieve the effect from PIL import Image, ImageDraw, ImageFont, ImageOps
def image_text():
image = Image.open('bg4.png')
draw = ImageDraw.Draw(image)
txt = Image.new('L', (500, 50))
font = ImageFont.truetype('demo.ttf', 20)
fillColor = "#0000ff"
text = "设"
size = (20, 20)
draw.text(size, text, font=font, fill=fillColor, direction=None)
w = txt.rotate(17.5, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 84)), (242, 60), w)
text = "来"
size = (0, 0)
draw.text(size, text, font=font, fill=fillColor, direction=None)
image.save('demo.png')
if __name__ == '__main__':
image_text() |
Draw.text() Wouldn't it be much better to add an angle parameter to the method and return the coordinate point |
In your code, replace the first draw.text(size, text, font=font, fill=fillColor, direction=None) with d = ImageDraw.Draw(txt)
d.text(size, text, font=font, fill=255, direction=None) |
As for the idea of building this functionality into Pillow's |
Wouldn't it be less complicated if you made rotating text and inverting text two different methods |
Inverting text was just an additional example. My opinion is that this isn't a popular enough use case, and since you can achieve the effect with other Pillow methods, this isn't explicitly a problem. If you disagree though, you're free to create a PR with those changes and see how it goes. |
OK, thanks, I've figured out how to rotate the text |
The width of my image is 310 and the coordinate point x=150 is exceeded, why is this happening? from PIL import Image, ImageDraw, ImageFont, ImageOps
def image_text():
image = Image.open('bg4.png')
print(image.size) # (310, 155)
font = ImageFont.truetype('demo.ttf', 20)
# 文字旋转
text = "设"
x_y = (145, 0)
angle = 0
txt = Image.new('L', (310, 155))
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 84)), x_y, w)
image.show()
if __name__ == '__main__':
image_text() |
The coordinates are the same, only the angle has changed, why is the x-coordinate shifted? from PIL import Image, ImageDraw, ImageFont, ImageOps
def image_text():
image = Image.open('bg4.png')
print(image.size) # (310, 155)
font = ImageFont.truetype('demo.ttf', 20)
# 文字旋转
text = "设"
x_y = (290, 0)
angle = 20
txt = Image.new('L', (310, 155))
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 84)), (0, 0), w)
text = "转"
x_y = (290, 0)
angle = 50
txt = Image.new('L', (310, 155))
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 84)), (0, 0), w)
text = "旋"
x_y = (290, 0)
angle = 90
txt = Image.new('L', (310, 155))
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 84)), (0, 0), w)
image.show()
if __name__ == '__main__':
image_text() |
If you're asking why the pasted image is not contained within the original image, it's because you're pasting it at 145 + 310 = 455 |
It's because you're not just rotating the text, you're rotating the new images that you've placed the text inside. To demonstrate, if I modify your code slightly, it produces this image. from PIL import Image, ImageDraw, ImageFont, ImageOps
def image_text():
image = Image.open('bg4.png')
print(image.size) # (310, 155)
font = ImageFont.truetype('demo.ttf', 20)
# 文字旋转
text = "a"
x_y = (290, 0)
angle = 20
txt = Image.new('L', (310, 155), 255)
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (0, 255, 84)), (0, 0), w)
text = "b"
x_y = (290, 0)
angle = 50
txt = Image.new('L', (310, 155), 255)
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 0, 84)), (0, 0), w)
text = "c"
x_y = (290, 0)
angle = 90
txt = Image.new('L', (310, 155), 255)
d = ImageDraw.Draw(txt)
d.text(x_y, text, font=font, fill=200, direction=None)
w = txt.rotate(angle, expand=1)
image.paste(ImageOps.colorize(w, (0, 0, 0), (255, 255, 0)), (0, 0), w)
image.show()
if __name__ == '__main__':
image_text() |
@zhenzi0322 does that answer your questions? |
You may also find this helpful. from PIL import Image, ImageDraw, ImageFont
def draw_rotated_text(image, font, text, angle, x, y):
txt = Image.new(image.mode, font.getsize(text))
d = ImageDraw.Draw(txt)
d.text((0, 0), text, font=font, fill=(255, 0, 0))
txt = txt.rotate(angle, expand=1)
image.paste(txt, (int(x - txt.width/2), int(y - txt.height/2)), txt)
def image_text():
image = Image.open('bg4.png')
font = ImageFont.truetype('demo.ttf', 50)
draw_rotated_text(image, font, "zero", 0, image.width/2, image.height/2)
draw_rotated_text(image, font, "ninety", 90, image.width/2, image.height/2)
draw_rotated_text(image, font, "onetwenty", 120, image.width/2, image.height/2)
image.show()
if __name__ == '__main__':
image_text() |
Thank you |
Thank you |
I want to write some text on the picture, its rotation angle how to operate it, the rotation angle of the text are random
The text was updated successfully, but these errors were encountered: