2010-01-12 126 views
0

考慮下面的代碼在文本抖動: 從PIL進口圖片,ImageDraw,ImageFont使用PIL和TrueType字體

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') 

Here are the input and the output files 這是輸入

input http://img16.imageshack.us/img16/8229/73936270.png

,這是輸出

output http://img94.imageshack.us/img94/531/outj.png

如您所見,輸出圖像在文本週圍包含許多微紅的噪音。我怎樣才能消除這種抖動?

+0

首先,在旋轉之前和之後保存'lTxtImg'。比較圖像:他們都有紅色的「噪音」? 順便說一句,這個字體是*不*宋體,這是一個襯線字體。你的操作系統是什麼? – tzot 2010-02-01 23:51:11

回答

0

我建議將中間文本圖像寫入文件(文本,然後是旋轉後的文本),以便找出出現文物的地方。

另一種可能性可能是png編碼使用沒有灰度值的調色板,因此這些紅色是最接近可用的。我檢查了在imageshack上的文件的編碼,但它似乎沒問題,所以我不認爲這是問題。

相關問題