How do I draw text at an angle using python's PIL?

后端 未结 6 1366
暗喜
暗喜 2020-12-04 12:19

Using Python I want to be able to draw text at different angles using PIL.

For example, imagine you were drawing the number around the face of a clock. The number <

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 13:12

    It's also usefull to know our text's size in pixels before we create an Image object. I used such code when drawing graphs. Then I got no problems e.g. with alignment of data labels (the image is exactly as big as the text).

    (...)
    img_main = Image.new("RGB", (200, 200))
    font = ImageFont.load_default()
    
    # Text to be rotated...
    rotate_text = u'This text should be rotated.'
    
    # Image for text to be rotated
    img_txt = Image.new('L', font.getsize(rotate_text))
    draw_txt = ImageDraw.Draw(img_txt)
    draw_txt.text((0,0), rotate_text, font=font, fill=255)
    t = img_value_axis.rotate(90, expand=1)
    

    The rest of joining the two images together is already described on this page. When you rotate by an "unregular" angle, you have to improve this code a little bit. It actually works for 90, 180, 270...

提交回复
热议问题