Question

Consider the following code: from PIL import 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')

Here are the input and the output files This is the input

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

and this is the output

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

As you may see, the output image contains a lot of reddish noise around the text. How can I eliminate this dithering?

Was it helpful?

Solution

I suggest writing the intermediate text images to file (the text, then the rotated text), to isolate where the artefacts first appear.

One other possibility could be that the png encoding is using a pallete with no grayscale values, so those reds are the closest available. I checked the encoding of the files on imageshack though, and it seemed okay, so I don't think this is the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top