| ID | Creation date | March 19, 2007 | |
| Platform | S60 2nd Edition, S60 3rd Edition | Tested on devices | Nokia N96 |
| Category | Python | Subcategory | UI |
| Keywords (APIs, classes, methods, functions): graphics, image |
Image class of Python will load a transparent gif/png image just like a non-transparent one. But its blit() method accept a mask parameter ! The missing link is to create a mask automatically from the Image. It's typically the top-left pixel. You can use Image's getpixel() and combine them all as shown below.
def automask(im):
width, height = im.size # get image size
mask = Image.new(im.size, '1') # black and white
tran = im.getpixel((0,0))[0] # transparent top-left
for y in range(height):
line = im.getpixel([(x, y) for x in range(width)])
for x in range(width):
if line[x] == tran:
mask.point((x,y), 0) # mask on the point
return mask
# import modules
from graphics import Image
import appuifw, e32
# define quit function
def quit():
a.signal()
appuifw.app.exit_key_handler = quit
# open image
img = Image.open('E:\\Images\\img.gif')
# create mask
mask = automask(img)
# create canvas on application body
appuifw.app.body = canvas = appuifw.Canvas()
# clear canvas with yellow color
canvas.clear(0xDBDB70)
# show transparent image
canvas.blit(img, mask=mask)
a = e32.Ao_lock()
a.wait() # wait for exit
Following screenshots show the result of above code.
No related wiki articles found