简体   繁体   English

使用PIL在python 2.7中将文本写入图像

[英]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. 将“ Hello World”写成白色。 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. 输入代码“ Hello world”将用黑色书写。

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? 我有一个python脚本试图将“ Hello World”写到jpg图像上,但是当我保存文件时,没有文本写入其中,有什么想法吗? I'm using python 2.7 and Pillow. 我正在使用python 2.7和Pillow。

I've also had a look at this documentation: http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html 我也看了一下这个文档: 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. 以下代码对我有用,但我使用了python 3和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) 我没有字体,但是它似乎也可以与python 2.7和jpg图片一起使用,就像您的代码一样(但我没有字体)

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. 以下与您的代码(使用我的Ubuntu 16.04)最相似,也可以在图像上渲染文本。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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