Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Archived:How to display transparent image on canvas

From Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Tested with
Devices(s): Nokia N96
Platform(s): S60 2nd Edition, S60 3rd Edition
Keywords: graphics, image
Created: cyke64 (19 Mar 2007)
Last edited: hamishwillee (31 May 2013)



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.


The automask function

def automask(im):
width, height = im.size # get image size
mask =, '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

Example usage

# import modules
from graphics import Image
import appuifw, e32
# define quit function
def quit():
a.signal() = quit
# open image
img ='E:\\Images\\img.gif')
# create mask
mask = automask(img)
# create canvas on application body = canvas = appuifw.Canvas()
# clear canvas with yellow color
# 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.

Origional Image
Normal blit method
Transparent Image
This page was last modified on 31 May 2013, at 01:08.
52 page views in the last 30 days.