博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二、文字转成图片,图片上写字
阅读量:4133 次
发布时间:2019-05-25

本文共 1165 字,大约阅读时间需要 3 分钟。

文字转成图片:

from PIL import Image,ImageDraw,ImageFontdef text2Image(self):        text = u"这是一段测试文本,test 123。"         im = Image.new("RGB", (300, 50), (255, 255, 255))        dr = ImageDraw.Draw(im)        font = ImageFont.truetype(os.path.join("fonts", "msyh.ttf"), 14)                 dr.text((10, 5), text, font=font, fill="#000000")                 im.show()        im.save("t.png")

图片上添加文字:

from PIL import Image,ImageDraw,ImageFontdef addText2Image(self):        # get an image        base = Image.open('image2.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 drawing context        d = ImageDraw.Draw(txt)                fnt = ImageFont.truetype(font='FreeMono.ttf', size=40)                # draw text, half opacity        d.text((10, 10), "Hello", font=fnt, fill=ImageColor.colormap['red'])        # draw text, full opacity        d.text((10, 60), "World", font=fnt, fill=ImageColor.colormap['salmon'])                out = Image.alpha_composite(base, txt)        out.show()

效果如下:

效果展示

参考文章:

Example: Draw Partial Opacity Text

转载地址:http://ziivi.baihongyu.com/

你可能感兴趣的文章
第三十四章:格子取数问题
查看>>
第三十五章:完美洗牌算法
查看>>
第三十九章:最近公共祖先LCA问题
查看>>
HDU2586 How far away ?
查看>>
写在路上
查看>>
第一章 趣味数独
查看>>
程序员之路
查看>>
第三十九章续:区间最值RMQ问题
查看>>
第二章 趣味迷宫
查看>>
第三章 坦克大战
查看>>
HDU1085 Holding Bin-Laden Captive!
查看>>
HDU1133 Buy the Ticket
查看>>
HDU1715 大菲波数
查看>>
HDU2048 神、上帝以及老天爷
查看>>
HDU2049 不容易系列之(4)——考新郎
查看>>
HDU2068 RPG的错排
查看>>
HDU2079 选课时间
查看>>
HDU2082 找单词
查看>>
给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数
查看>>
some面试题一览
查看>>