1 #----------------------------------------------------------------------
2 # Name: wx.lib.embeddedimage
3 # Purpose: Defines a class used for embedding PNG images in Python
4 # code. The primary method of using this module is via
5 # the code generator in wx.tools.img2py.
7 # Author: Anthony Tuininga
10 # RCS-ID: $Id: embeddedimage.py 59672 2009-03-20 20:59:42Z RD $
11 # Copyright: (c) 2007 by Anthony Tuininga
12 # Licence: wxWindows license
13 #----------------------------------------------------------------------
20 b64decode = base64.b64decode
21 except AttributeError:
22 b64decode = base64.decodestring
25 class PyEmbeddedImage(object):
27 PyEmbeddedImage is primarily intended to be used by code generated
28 by img2py as a means of embedding image data in a python module so
29 the image can be used at runtime without needing to access the
30 image from an image file. This makes distributing icons and such
31 that an application uses simpler since tools like py2exe will
32 automatically bundle modules that are imported, and the
33 application doesn't have to worry about how to locate the image
34 files on the user's filesystem.
36 The class can also be used for image data that may be acquired
37 from some other source at runtime, such as over the network or
38 from a database. In this case pass False for isBase64 (unless the
39 data actually is base64 encoded.) Any image type that
40 wx.ImageFromStream can handle should be okay.
43 def __init__(self, data, isBase64=True):
45 self.isBase64 = isBase64
48 return wx.BitmapFromImage(self.GetImage())
52 data = b64decode(self.data)
57 icon.CopyFromBitmap(self.GetBitmap())
61 stream = cStringIO.StringIO(self.GetData())
62 return wx.ImageFromStream(stream)
64 # added for backwards compatibility
70 # define properties, for convenience
71 Bitmap = property(GetBitmap)
72 Icon = property(GetIcon)
73 Image = property(GetImage)