简体   繁体   中英

Writing text to image in python 2.7 using PIL

Edit: Code works however I had

draw.text((30,10), "Hello World", (255, 255, 255, 255),font=font)

which writes "Hello World" as white. If you change it to

draw.text((30,10), "Hello World", (255, 255, 255, 255),font=font)

enter code "Hello world" will be written in black.

I have a python script that is trying to write "Hello World" onto a jpg image but when I save the file there is no text written to it, any ideas? I'm using python 2.7 and Pillow.

I've also had a look at this documentation: http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html

Code:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

pattern = Image.open("DANK.jpg", "r").convert('RGBA') 

size = width, height = pattern.size
draw = ImageDraw.Draw(pattern,'RGBA')
font = ImageFont.truetype("Font.ttf", 3)

draw.text((30,10), "Hello World", (255, 255, 255, 255),font=font)
pattern.save('sample-out.jpg')

I've also noticed that when the image is saved the colours become gray scale, why might that be?

The following code worked for me but I used python 3 and a png. I placed the image in the same directory as the code and then a text was written over the image.

from PIL import Image, ImageDraw, ImageFont
# get an image
base = Image.open('lena.png').convert('RGBA')

# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))

# get a font
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 40)
# get a drawing context
d = ImageDraw.Draw(txt)

# draw text, half opacity
d.text((100,100), "Hello", font=fnt, fill=(255,255,255,128))
# draw text, full opacity
d.text((100,160), "World", font=fnt, fill=(255,255,255,255))

out = Image.alpha_composite(base, txt)

out.show()

I don't have the font, but it also appears to work with python 2.7 and a jpg image just like your code (but I don't have the font)

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

pattern = Image.open("DANK.jpg", "r").convert('RGBA')

size = width, height = pattern.size
draw = ImageDraw.Draw(pattern,'RGBA')
#font = ImageFont.truetype("Font.ttf", 3)

draw.text((30,10), "Hello World", (0, 0, 0, 0))#,font=font)
pattern.save('sample-out.jpg')

在此处输入图片说明

The most similar to your code (using my Ubuntu 16.04) is the following which also renders text on the image.

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

pattern = Image.open("DANK.jpg", "r").convert('RGBA')

size = width, height = pattern.size
draw = ImageDraw.Draw(pattern,'RGBA')
font = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 100)

draw.text((300,10), "Hello World", (0, 0, 0, 0),font=font)
pattern.save('sample-out.jpg')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM