How to draw unicode characters on transparent image in PIL
Asked Answered
B

3

3

I am trying to draw certain unicode characters on and image using python (PIL to be precise).

Using the following code I can generate the images with a white background:

('entity_code' is passed in to the method)

    size = self.font.getsize(entity_code)
    im = Image.new("RGBA", size, (255,255,255))
    draw = ImageDraw.Draw(im)
    draw.text((0,6), entity_code, font=self.font, fill=(0,0,0))
    del draw
    img_buffer = StringIO()
    im.save(img_buffer, format="PNG")

I tried the following:

('entity_code' is passed in to the method)

    img = Image.new('RGBA',(100, 100))
    draw = ImageDraw.Draw(img)
    draw.text((0,6), entity_code, fill=(0,0,0), font=self.font)
    img_buffer = StringIO()
    img.save(img_buffer, 'GIF', transparency=0)

This however fails to draw the unicode character. It looks like I end up with an empty transparent image :(

What am I missing here? Is there a better way to draw text on a transparent image in python?

Beatriz answered 21/6, 2012 at 6:45 Comment(0)
D
2

Your code examples are all over the place, and I am inclined to agree with @fraxel that you are not being specific enough in the usage of fill colors and the background colors for your RGBA images. However I can't actually get your code example to work at all because I don't really have any idea of how your code fits together.

Also, just like @monkut mentioned you need to look at the font you are using because your font may not support specific unicode characters. However unsupported characters should be drawn as a empty square (or whatever the default value is) so you would at least see some kind of output.

I have created a simple example below that draws unicode characters and saves them to a .png file.

import Image,ImageDraw,ImageFont

# sample text and font
unicode_text = u"Unicode Characters: \u00C6 \u00E6 \u00B2 \u00C4 \u00D1 \u220F"
verdana_font = ImageFont.truetype("verdana.ttf", 20, encoding="unic")

# get the line size
text_width, text_height = verdana_font.getsize(unicode_text)

# create a blank canvas with extra space between lines
canvas = Image.new('RGB', (text_width + 10, text_height + 10), (255, 255, 255))

# draw the text onto the text canvas, and use black as the text color
draw = ImageDraw.Draw(canvas)
draw.text((5,5), unicode_text, font = verdana_font, fill = "#000000")

# save the blank canvas to a file
canvas.save("unicode-text.png", "PNG")

The code above creates the png displayed below: unicode text

As a side note, I am using Pil 1.1.7 and Python 2.7.3 on Windows.

Dachy answered 21/1, 2013 at 19:55 Comment(0)
M
0

I think you have to make sure that your loaded font supports the characters your trying to output.

An example here: http://blog.wensheng.com/2006/03/how-to-create-images-from-chinese-text.html

font = ImageFont.truetype('simsun.ttc',24)
Maxma answered 21/6, 2012 at 7:59 Comment(0)
M
0

In your examples you create a RGBA image, but you don't specify the value of the alpha channel (so it defaults to 255). If you replace (255, 255, 255) with (255,255,255,0) it should work ok (as pixels with 0 alpha are transparent).

To illustrate:

import Image
im = Image.new("RGBA", (200,200), (255,255,255))
print im.getpixel((0,0))
im2 = Image.new("RGBA", (200,200), (255,255,255,0))
print im2.getpixel((0,0))
#Output:
(255, 255, 255, 255)
(255, 255, 255, 0)
Milium answered 21/6, 2012 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.