python - 使用多种颜色创建图像并添加文本 [英] python - create image with multiple colors and add text

查看:416
本文介绍了python - 使用多种颜色创建图像并添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python中的某些文本创建一个图像,如:

I am trying to create an image with some text in python like:

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf",25)
img=Image.new("RGBA", (200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)
img.save("test.png")

这很好用,但是,我无法添加更多颜色,例如3种不同颜色的RED,YELLOW,GREEN就像信号量一样。

This works fine, although, I was unable to add more colors, for instance 3 different colors RED,YELLOW, GREEN that will be like a semaphore.

此外,可以转换图像,例如转角吗?

Also, can the image be transformed, for instance round the corners?

非常感谢

推荐答案

您可以在 ImageDraw.Draw中使用和弦调用 class。你必须通过这个函数四个参数,第一个是位置和大小,描述为一个封闭的盒子,即(x1,y1,x2,y2),第二个是起始角度,第三个是结束角度;如果你想要一个完整的圆圈,你必须分别指定0和360。您可以使用命名参数 fill 设置填充颜色。

You can use the chord call at the ImageDraw.Draw class. You must pass this function four parameters, first one is position and size, described as an enclosing box, i.e. (x1, y1, x2, y2), second one is the start angle and the third one is the end angle; if you want a full circle you must specify 0 and 360, respectively. You can set the fill colour with a named parameter fill.

使用示例更好地解释:

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

font = ImageFont.truetype("/usr/share/fonts/truetype/DejaVuSans.ttf",25)
img=Image.new("RGBA", (200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw.chord((100, 75, 125, 100), 0, 360, fill='green')
draw.chord((75, 100, 100, 125), 0, 360, fill='blue')
draw.chord((125, 125, 150, 150), 0, 360, fill='yellow')
draw = ImageDraw.Draw(img)

img.save("test.png")

这篇关于python - 使用多种颜色创建图像并添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆