Archived:How to draw an icon on canvas in PySymbian

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
Created: cyke64 (15 Mar 2007)
Last edited: hamishwillee (31 May 2013)

Python have 2 different classes for graphics data :

  • Icon class represents icon (typically from .mbm file) which can be put in ListBox for selection.
  • Image class represents a bigger image which can be drawn upon.

These 2 classes can't convert to/from each other.

But you can use a library that read .mbm file and draw an Image from the data there. So, you can make an icon into an image the module icon_image whose source code is below can be used for this purpose.

# module icon_image from Korakot user
# called by just import icon_image
# im = icon_image(file_mbm, idx)
from graphics import Image
from struct import unpack
def readL(f, pos=None):
if pos is not None:
return unpack('L', f.read(4))[0]
def open(file_mbm='z:\\system\\data\\avkon.mbm', idx = 28):
# read icon data from mbm file
f = file(file_mbm, 'rb')
if readL(f) != 0x10000041:
return None # work for mbm on ROM (z:) only
start = readL(f, 8+4*idx)
length = readL(f) - readL(f) # pd_size - offset
width, height = readL(f), readL(f)
enc = readL(f, start+56)
data_encoded = f.read(length)
# decode the data
data_padded = rle_decode(data_encoded, enc)
mat = bit_matrix(data_padded, width, height)
im = Image.new((width, height), '1')
for j in range(height):
for i in range(width):
im.point((i,j), mat[j][i]*0xffffff)
return im
# Decode of 8-bit RLE
# Either to repeat-(n+1)-times or not repeat (100-n) bytes
def rle_decode(bytes, enc=1):
if not enc: return bytes
out = []
i = 0
while i < len(bytes):
n = ord(bytes[i])
i += 1
if n < 0x80:
out.append( bytes[i] * (n+1) )
i += 1
n = 0x100 - n
out.append( bytes[i:i+n] )
i += n
return ''.join(out)
# from bytes to bit matrix
# Each line was padded to 4-byte boundary, must discard the end
def bit_matrix(bytes, width, height):
mat = []
k = 0
for j in range(height):
line = []
while len(line)<width:
longint, = unpack('L', bytes[k: k+4])
k += 4
toget = min(width - len(line), 32)
for i in range(toget):
longint, r = divmod(longint, 2)
return mat

Here's an example using this module.

  • Python 2nd Edition
from appuifw import *
import icon_image, e32
app.body = c = Canvas()
# choose a bitmap from plenty inside mbm (multi-bitmap)
icon = icon_image('z:\\system\\data\\avkon.mbm', 28)
e32.ao_sleep(10) # 10 sec to end program
  • Python 3rd Edition
from appuifw import *
import icon_image, e32
app.body = c = Canvas()
# choose a bitmap from plenty inside mbm (multi-bitmap)
icon = icon_image('z:\\system\\data\\avkon2.mbm', 28)
e32.ao_sleep(10) # 10 sec to end program

Limitations : only 1-bit icons.

This page was last modified on 31 May 2013, at 04:03.
59 page views in the last 30 days.