Skip to content
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

Closed
zhenzi0322 opened this issue Sep 29, 2021 · 16 comments
Closed

How does writing text in images with random angles work? #5738

zhenzi0322 opened this issue Sep 29, 2021 · 16 comments
Labels

Comments

@zhenzi0322
Copy link

I want to write some text on the picture, its rotation angle how to operate it, the rotation angle of the text are random

@hugovk
Copy link
Member

hugovk commented Sep 29, 2021

Does this help?

https://stackoverflow.com/q/245447/724176

@zhenzi0322
Copy link
Author

zhenzi0322 commented Sep 29, 2021

有帮助吗?

https://stackoverflow.com/q/245447/724176

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()

@zhenzi0322
Copy link
Author

Does this help?

https://stackoverflow.com/q/245447/724176

Draw.text() Wouldn't it be much better to add an angle parameter to the method and return the coordinate point

@radarhere
Copy link
Member

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)

@radarhere
Copy link
Member

As for the idea of building this functionality into Pillow's text() method directly, there doesn't seem to be a particular need for this. You might be interested in rotating text, but someone else might be interested in writing text upside down, or back to front. If we added all of these different options as text() arguments, it would become very complicated.

@zhenzi0322
Copy link
Author

As for the idea of building this functionality into Pillow's text() method directly, there doesn't seem to be a particular need for this. You might be interested in rotating text, but someone else might be interested in writing text upside down, or back to front. If we added all of these different options as text() arguments, it would become very complicated.

Wouldn't it be less complicated if you made rotating text and inverting text two different methods

@radarhere
Copy link
Member

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.

@zhenzi0322
Copy link
Author

在代码中,替换第一个

draw.text(size, text, font=font, fill=fillColor, direction=None)

d = ImageDraw.Draw(txt)
d.text(size, text, font=font, fill=255, direction=None)

OK, thanks, I've figured out how to rotate the text

@zhenzi0322
Copy link
Author

zhenzi0322 commented Sep 30, 2021

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.

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()

@zhenzi0322
Copy link
Author

zhenzi0322 commented Sep 30, 2021

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()

@radarhere
Copy link
Member

The width of my image is 310 and the coordinate point x=150 is exceeded, why is this happening?

If you're asking why the pasted image is not contained within the original image, it's because you're pasting it at x_y, where the x coordinate is 145.

145 + 310 = 455

@radarhere
Copy link
Member

The coordinates are the same, only the angle has changed, why is the x-coordinate shifted?

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.

out

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()

@radarhere
Copy link
Member

@zhenzi0322 does that answer your questions?

@radarhere
Copy link
Member

radarhere commented Oct 2, 2021

You may also find this helpful.

out

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()

@zhenzi0322
Copy link
Author

Thank you

@zhenzi0322
Copy link
Author

@zhenzi0322 does that answer your questions?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants