I need to write text to image and this is how I did it:
image = Image.open(background)
draw = ImageDraw.Draw(image)
text = str(randint(0, 9999999))
# specified font size
fs = 52
font = ImageFont.truetype('font.ttf', 52)
font_size = font.getsize(text)
draw.text((x, y), text, fill =(64,64,64), font = font, align ="right")
But I needed to rotate this text and I couldn't find anything except to create an image write text to it, rotate this image and then paste it to original image.
txt=Image.new('L', (w,h))
d = ImageDraw.Draw(txt)
d.text( (0, 0), text, font=font, fill=186)
w=txt.rotate(-3, expand=1)
px, py = x, y
sx, sy = w.size
image.paste(w, (px, py, px + sx, py + sy), w)
Problem is that I can't fill it with the same gray color that I did with earlier text. In other words I want to assign it a gray color but can't do it. If I create this image as RGB then it doesn't mask well to the original image and gives error.