当前位置: > > > > 如何向任何图像添加带有黑色阴影的白色文本?
来源:stackoverflow
2024-04-23 09:54:35
0浏览
收藏
最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何向任何图像添加带有黑色阴影的白色文本?》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~
问题内容
我正在使用 github.com/fogleman/gg
在图像上绘制一些白色文本:
existing, _ := gg.LoadJPG(path) dc := gg.NewContextForImage(existing) dc.SetRGB(0, 0, 0) dc.DrawString(word, x, y) dc.SetRGB(1, 1, 1) dc.DrawString(word, x-3, y-3)
有时它看起来很完美:
但有时这很糟糕:
有什么技巧可以使其在任何图像上都可读吗?
正确答案
哦,我想我知道该怎么做了:
你必须为每个字母制作一个巨大的黑色背景,而不仅仅是一个微小的阴影。
for i := 0; i < 100; i++ { angle := float64(i) / float64(smoothness) * 2 * math.pi xoffset := math.cos(angle) * 4 yoffset := math.sin(angle) * 4 dc.drawstringanchored(letter, x+xoffset, y+yoffset, 0.5, 0.5) }
更新:在 python 中执行此操作更容易:
pip3 install pillow
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageChops import sys import random filename = sys.argv[1] x = int(sys.argv[2]) y = int(sys.argv[3]) text = sys.argv[4] result = sys.argv[5] image = Image.open(filename) bg = ImageDraw.Draw(image) font = ImageFont.truetype('font.ttf',96) colors = ['white', 'pink', 'green', 'orange', 'red', 'yellow', 'purple', 'cyan'] fill_color = random.choice(colors) ranX = random.randint(-10, 10) ranY = random.randint(-10, 10) bg.text((x + ranX, y + ranY), text, fill=fill_color, font=font, spacing = 4, align = 'center', stroke_width=8, stroke_fill='black') image.save(result)
理论要掌握,实操不能落!以上关于《如何向任何图像添加带有黑色阴影的白色文本?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注公众号吧!