conveniently extracting images for img2py file

Hi
What is a convenient way to get an image from a large py file?
I am looking for an function like def getImage(imageName).

The icon file looks like:
# This file was generated by img2py.py
from wx.lib.embeddedimage import PyEmbeddedImage
a_exitnosave = PyEmbeddedImage( "iVBORw0K < CUT........>")
geta_exitnosaveImage = a_exitnosave.GetImage

and has like 30 icons. So there are 30 times lines like get{SOMETHING}
Image. This is not easy for use.
I made the following function:

def getIconFromPy(iconName, size=(48,48)):
    d = {}
    exec "def f(): return " + iconName+ ".GetImage()" in globals(),d
    image = d['f']()
    return image.Rescale(size[0], size[1])

which I can call with image=getIconFromPy("a_markerror"). This works,
but I find the code a bit complicated and feel like there must be a
better way.
Is there? BTW: the file is as is. I don't intend to regenerate a new
file.

Tia Samuel

Samuel,

Hi
What is a convenient way to get an image from a large py file?
I am looking for an function like def getImage(imageName).

The icon file looks like:
# This file was generated by img2py.py
from wx.lib.embeddedimage import PyEmbeddedImage
a_exitnosave = PyEmbeddedImage( "iVBORw0K< CUT........>")
geta_exitnosaveImage = a_exitnosave.GetImage

and has like 30 icons. So there are 30 times lines like get{SOMETHING}
Image. This is not easy for use.
I made the following function:

def getIconFromPy(iconName, size=(48,48)):
     d = {}
     exec "def f(): return " + iconName+ ".GetImage()" in globals(),d
     image = d['f']()
     return image.Rescale(size[0], size[1])

which I can call with image=getIconFromPy("a_markerror"). This works,
but I find the code a bit complicated and feel like there must be a
better way.
Is there? BTW: the file is as is. I don't intend to regenerate a new
file.
   
If you use a .py file which was generated with img2py.py then you just need:

import myimages

myimages.get"YourIcon/imageName"Image()

Or if you use the newer format (see -f, -F switch for img2py), then you could do:

myimages.yourImageName.GetImage()
myimages.yourImageName.GetBitmap()
myimages.yourImageName.GetIcon()

Or do I not understand your problem correctly?

Werner

···

On 02/04/2010 12:32, samuel en wrote:

myimages.get"YourIcon/imageName"Image()

Or if you use the newer format (see -f, -F switch for img2py), then you
could do:

myimages.yourImageName.GetImage()
myimages.yourImageName.GetBitmap()
myimages.yourImageName.GetIcon()

Or do I not understand your problem correctly?

Werner

Either you didn't understood my question, or I made horrible mistakes
when testing the code (that happens).
The point is: I really don't want to use myimages.get"YourIcon/
imageName"Image() since then there are more than 30 "hardcoded"
function calls from the module where I import the img2py file.
I would like to use a variable like iconName ="YourIcon/imageName" ,
myimages.get<iconName>Image().
Like in exec "def f(): return " + iconName+ ".GetImage()" in
globals(),d

All the images/icons have logical names (for me that is), like
arrowR_red, and arrowL_green and I just want to use these names and
not explicitly write x = myimages.arrowR_red.GetImage, but just x=
getImage("arrowR").

BTW: It goes too far to explain fully why I exactly what to
implemented it this way (some reasons are the way I switch during
development from reading icons from disk instead of from a py file
back and forth, other reason is to keep the main module code clean,
other reason is that the iconnames sometimes are generated run time).
Off course there working arounds for all these things (but =>more
code).

Hi,

All the images/icons have logical names (for me that is), like
arrowR_red, and arrowL_green and I just want to use these names and
not explicitly write x = myimages.arrowR_red.GetImage, but just x=
getImage("arrowR").

Write a function

def getImage(imgName):
    return getattr(myimages, imgName).GetImage()

Or

py2img also generates a catalog dictionary in the module it outputs.

myimages.catalog[imgName].GetImage()

Cody

···

On Fri, Apr 2, 2010 at 7:48 AM, samuel en <samuel110011@gmail.com> wrote:

This should be close then:

myicons = ['rect', 'save']
for icon in myicons:
     x = getattr(myimages, icon)
     x.GetBitmap()

This gives me this in the shell:
<wx._gdi.Bitmap; proxy of <Swig Object of type 'wxBitmap *' at 0xc3fe780> >
<wx._gdi.Bitmap; proxy of <Swig Object of type 'wxBitmap *' at 0xc3fe870> >

Werner

···

On 02/04/2010 14:48, samuel en wrote:

myimages.get"YourIcon/imageName"Image()

Or if you use the newer format (see -f, -F switch for img2py), then you
could do:

myimages.yourImageName.GetImage()
myimages.yourImageName.GetBitmap()
myimages.yourImageName.GetIcon()

Or do I not understand your problem correctly?

Werner
     

Either you didn't understood my question, or I made horrible mistakes
when testing the code (that happens).
The point is: I really don't want to use myimages.get"YourIcon/
imageName"Image() since then there are more than 30 "hardcoded"
function calls from the module where I import the img2py file.
I would like to use a variable like iconName ="YourIcon/imageName" ,
myimages.get<iconName>Image().
Like in exec "def f(): return " + iconName+ ".GetImage()" in
globals(),d

All the images/icons have logical names (for me that is), like
arrowR_red, and arrowL_green and I just want to use these names and
not explicitly write x = myimages.arrowR_red.GetImage, but just x=
getImage("arrowR").

Hi Cody & Werner.
I was totally unaware of existence of any getattr function!
Couldn't test it yet, but I am sure this will simplify my code and
will yield the same result.
Thanks,
Samuel

samuel en wrote:

Hi Cody & Werner.
I was totally unaware of existence of any getattr function!
Couldn't test it yet, but I am sure this will simplify my code and
will yield the same result.
Thanks,
Samuel

also of use is setattr and hasattr. Python's introspection rocks and can
be incredibly useful to achieve polymorphic behaviour.

···

--
Steven Sproat, BSc
http://www.launchpad.net/whyteboard