[英]pil draw text with different colors
嗨,用不同的選項為 ex 繪制三個不同的文本:
文本必須換行。
def pil_image(request):
text = request.GET.get('text', None)
font = str(request.GET.get('font', 'arial'))
fontsize = int(request.GET.get('fontsize', '20'))
textcolor = str(request.GET.get('textcolor', '000'))
import Image, ImageDraw, ImageFont, textwrap
img = Image.open('media/text/transparent.png')
img = img.convert("RGBA")
datas = img.getdata()
w, h = img.size
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("media/text/fonts/" + font + ".ttf", fontsize, encoding="unic")
margin = offset = 40
for line in textwrap.wrap(text, width=48):
w, h = draw.textsize(line)
draw.text((margin, offset), line, font=font, fill='#'+textcolor)
offset += font.getsize(line)[1]
del draw
img.save("media/text/custom.png", "PNG")
return HttpResponse("<img src='/media/text/custom.png'>");
“fill”參數應該是一個有 4 個數字的元組,因為你使用的是 RGBA 顏色模式。
對於不透明的紅色:
draw.text((margin, offset), line, font=font, fill=(255,0,0,255) )
使用十六進制值作為顏色,如下所示:
draw.text((margin, offset), line, font=font, fill="#000")
簡單地做這個
# thicker border
draw.text((x-1, y-1), text, font=font, fill=shadowcolor)
draw.text((x+1, y-1), text, font=font, fill=shadowcolor)
draw.text((x-1, y+1), text, font=font, fill=shadowcolor)
draw.text((x+1, y+1), text, font=font, fill=shadowcolor)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.