×
Namespaces

Variants
Actions

Archived:How to display transparent image on canvas

From Nokia Developer 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
Compatibility
Platform(s): S60 2nd Edition, S60 3rd Edition
Article
Keywords: graphics, image
Created: cyke64 (19 Mar 2007)
Last edited: hamishwillee (31 May 2013)


Contents

Overview

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.

Code

The automask function

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

Example usage

# 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

Postconditions

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 04:08.
76 page views in the last 30 days.
×