[英]Dithering in text using PIL and truetype fonts
考慮以下代碼:從PIL導入Image,ImageDraw,ImageFont
def addText(img, lTxt):
FONT_SIZE = 10
INTERLINE_DISTANCE = FONT_SIZE + 1
font = ImageFont.truetype('arial.ttf', FONT_SIZE)
lTxtImageHeight = INTERLINE_DISTANCE * len(lTxt)
# create text image
lTxtImg = Image.new('RGBA', (img.size[1], lTxtImageHeight), 255)
lTxtImgDraw = ImageDraw.Draw(lTxtImg, )
for (i, line) in enumerate(lTxt):
lTxtImgDraw.text((5, i * INTERLINE_DISTANCE), line, font=font, fill='#000000')
# rotate text image
lTxtImg = lTxtImg.rotate(90)
# create new transparent image ret
ret = Image.new('RGBA', (img.size[0] + lTxtImageHeight, img.size[1]), 255)
# paste the image to ret
ret.paste(img, (0,0))
# paste the text to ret
ret.paste(lTxtImg, (img.size[0], 0), lTxtImg)
return ret
img = Image.open('in.png')
addText(img, ['lorem', 'ipsum', 'dolores']).save('out.png')
這是輸入和輸出文件,這是輸入
輸入http://img16.imageshack.us/img16/8229/73936270.png
這是輸出
輸出http://img94.imageshack.us/img94/531/outj.png
您可能會看到,輸出圖像在文本周圍包含許多微紅色的噪點。 如何消除這種抖動?
我建議將中間文本圖像寫入文件(文本,然后是旋轉的文本),以隔離文物首次出現的位置。
另一種可能是png編碼正在使用沒有灰度值的調色板,因此這些紅色是最接近的可用顏色。 我檢查了imageshack上文件的編碼,看起來還可以,所以我認為這不是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.