Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 3 would appear as expected whereas 12 would we drawn rotated counter-clockwise 90 degrees.

Therefore, I need to be able to draw many different strings at many different angles.

like image 853
carrier Avatar asked Oct 29 '08 01:10

carrier


People also ask

How do you draw a line in a pillow Python?

line() Draws a line between the coordinates in the xy list. Parameters: xy – Sequence of either 2-tuples like [(x, y), (x, y), …] or numeric values like [x, y, x, y, …].

How do you display the PIL of an image in Python?

Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image. Once the program execution is completed, the temporary file will be deleted.


1 Answers

Draw text into a temporary blank image, rotate that, then paste that onto the original image. You could wrap up the steps in a function. Good luck figuring out the exact coordinates to use - my cold-fogged brain isn't up to it right now.

This demo writes yellow text on a slant over an image:

# Demo to add rotated text to an image using PIL  import Image import ImageFont, ImageDraw, ImageOps  im=Image.open("stormy100.jpg")  f = ImageFont.load_default() txt=Image.new('L', (500,50)) d = ImageDraw.Draw(txt) d.text( (0, 0), "Someplace Near Boulder",  font=f, fill=255) w=txt.rotate(17.5,  expand=1)  im.paste( ImageOps.colorize(w, (0,0,0), (255,255,84)), (242,60),  w) 
like image 188
DarenW Avatar answered Oct 07 '22 10:10

DarenW