Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Korakot Chaovavanich http://korakot.stumbleupon.com

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Show all icons in avkon.mbm

There are 2 type of mbm files. The following code work with
ROM-type (most mbm on Z:\) only.
   1  
   2  >>> from appuifw import *
   3  >>> avkon = u'z:\\system\\data\\avkon.mbm'
   4  >>> def showicon(id_list, maskfunc=lambda x:x):
   5  ...     if type(id_list) == int:
   6  ...         id_list = [id_list] # one item
   7  ...     entries = []
   8  ...     if type(maskfunc) == dict:  # allow dict as a func
   9  ...         func = lambda id: maskfunc.get(id,id)
  10  ...     else:
  11  ...         func = maskfunc
  12  ...     for id in id_list:
  13  ...         item = u'%s, %s' % (id, func(id))
  14  ...         icon = Icon(avkon, id, func(id))
  15  ...         entries.append((item, u'', icon))
  16  ...     app.body = Listbox(entries, lambda: None)
  17  ...
  18  >>> showicon(range(503))  # all icons, including mask
  19  >>> showicon(range(0,100,2), lambda x:x+1)  # first 50 icons, with their mask brother

Using transparent gif/png with Image's blitting

pys60's Image class will load a transparent gif/png
just like a non-transparent one.
But its blit() method accept a mask paramenter.
I have talked about this in a previous snippet.

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() which is undocumented.
I show typical use of getpixel here.

Combine them all, here's the automask function.
   1  
   2  def automask(im):
   3      width, height = im.size
   4      mask = Image.new(im.size, '1') # black and white
   5      tran = im.getpixel((0,0))[0]   # transparent top-left
   6      for y in range(height):
   7          line = im.getpixel([(x, y) for x in range(width)])
   8          for x in range(width):
   9              if line[x] == tran:
  10                  mask.point((x,y), 0)  # mask on the point
  11      return mask

An example usage
   1  
   2  from graphics import Image
   3  
   4  ship = Image.open('E:\\Images\\ship.gif')
   5  mask = automask(ship)
   6  
   7  canvas.blit(ship, mask=mask)  # don't forget to create canvas first

A Sprite class can be defined based on this too.

Image/canvas bliting

I can't remember how to call the blit method.
So, here's just a personal reminder.
(BTW, the pys60 doc has a wrong order between target and source)
   1  
   2  app.body = c = Canvas()
   3  w,h = 20,20
   4  im = Image.new(size=(w,h))
   5  
   6  # here's prototype
   7  # blit(im, source=(0,0,w,h), target=(0,0), mask=None, scale=0)
   8  # here are examples
   9  c.blit(im)  # put entire image on top-left
  10  c.blit(im, target=(70,70)) # put it about mid-screen
  11  c.blit(im, (0,0,w/2,h/2))  # put a quater image on top-left
  12  
  13  # double the image size and put it about mid-screen
  14  c.blit(im, target=(60,60,100,100), scale=1)

If scale is not specified and source <> target, the image
will be clipped instead of resized.

mask is a 1-bit image with the same size as image.
   1  mask = Image.new((w,h),'1')

Pixel on the image will be copied if the same pixel
on the mask is 'white'.
So we can use mask to make 'Sprite'.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS