I am a beginner programmer who is moving on from the basics of python to wxPython. I have the book wxPython in Action and am using it as a reference while I make GUI programs for fun (still haven't sat down and read through the book, will someday). I am figuring everything out fine EXCEPT for loading images. I can't for the life of me figure this out. I keep getting this error:

Traceback (most recent call last):
File "C:\Python26\testimage.py", line 14, in <module>
frame = Frame()
File "C:\Python26\testimage.py", line 10, in __init__
sb1 = wx.StaticBitmap(p, -1, wx.BitmapFromImage(img1))
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 823, in BitmapFromImage
val = _gdi_.new_BitmapFromImage(*args, **kwargs)
PyAssertionError: C++ assertion "image.Ok()" failed at ..\..\src\msw\bitmap.cpp(802) in wxBitmap::CreateFromImage(): invalid image

...no matter what I do. I am so confused, does the image have to be in a specific place for me to load it? Am I using bad images? I tried a simple example out of the book, the image is on my desktop, but it keeps saying invalid image, even when I try new images. Any ideas on what I'm doing wrong?

Not possible to help you if you dont post the code.

Look at this.
http://wxpython.webs.com/tutorial5.htm

Install wxpython demo.
I have made put files in folder named c:\demo.
To start python c:\demo\demo\demo.py

I am not really sure, but what type of image are you trying? jpg, gif, etc...

Here is an example of the simplest way to show an image with wxPython ...

# really simple way to show an image from a file with wxPython
# pick an image file you have in the working folder
# or give the image file a full path name
# (can be a .jpg, .png, ,gif, .bmp image file)
# for instance: 'Clouds.jpg'
# note that filenames are case sensitive in Linux
# adjust the frame size to the picture size manually
# zoe --> Zzucker

import wx

app = wx.App(0)
frame = wx.Frame(None, -1, "Show an image file", size=(640, 480))
wx.StaticBitmap(frame, -1, wx.Bitmap('Clouds.jpg'))
frame.Show()
app.MainLoop()

For a more detailed class-based version see:
http://www.daniweb.com/forums/post624835.html#post624835

If you use code from the book, be aware that the code given there IMHO is somewhat clumsy.

Thanks for the responses guys. I tried vegaseat's way and got it to work, once, when I put the file in the Python26 folder. All the other times it told me it couldn't find the image, even when I did the same thing. I will attach some code that I tried. I basically am copying this out of the book. Perhaps I am missing something.

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Image Window")
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour("White")

        #Here I show what the book shows me

        bground = wx.Image('sonic.gif',wx.BITMAP_TYPE_ANY)
        bground1 = wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(bground))
        


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.Show()
    app.MainLoop()

I have figured out that I get the invalid image error when I use wx.Image() and I get "Can't find Image" when I don't. Perhaps when I am not using wx.Image() I am just putting the image in the wrong place. Any suggestions?

That code work.
You have off course to have image in same folder as you run your.py file from.

Or give it correct path name to file.
bground = wx.Image('c:/myfolder/sonic.gif',wx.BITMAP_TYPE_ANY)

That code work.
You have off course to have image in same folder as you run your.py file from.

Or give it correct path name to file.
bground = wx.Image('c:/myfolder/sonic.gif',wx.BITMAP_TYPE_ANY)

This ended up working, thanks! Thanks to everyone who replied.

You get invalid image if the program you posted does not find the image file. For those of you who use Linux, the file name is also case sensitive.

Well, I got the image to work. Now I have one more related question. When I put the image in my panel it automatically puts it in the top left corner. This worked for my top banner, but i'd like to put an image in the bottom left and bottom right corners of my panel. I am having trouble finding a method that can do this with my image. Is there a way to move the image or position it in my panel like I can do with my text?

Can you show us your final code?

I assume what you talking about is something like this ...

# position upper left corner of image at x=20, y=10
        bground1 = wx.StaticBitmap(self.panel, -1, 
            wx.BitmapFromImage(bground), pos=(20, 10))

Can you show us your final code?

I assume what you talking about is something like this ...

# position upper left corner of image at x=20, y=10
        bground1 = wx.StaticBitmap(self.panel, -1, 
            wx.BitmapFromImage(bground), pos=(20, 10))

Yes, this was exactly what I was looking for! Thanks. Again, I must ask one more question. I put my file into a folder, put the images in that folder, but I still am having a problem. 2 of the images (which I coded into my program at about the same time) are working, however, I can no longer add new images to my program without getting that annoying invalid image error. I don't understand, I have the code written exactly the same except I change the name of the image.

Here is the code from the part that is giving me trouble:

bleft = wx.Image('C:\Users\Alexander\Desktop\New Folder\left.jpg',
                         wx.BITMAP_TYPE_ANY)
        bleft.Rescale(100,100)
        bleft2 = wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(bleft),
                                 pos = (0,540))

        bright = wx.Image('C:\Users\Alexander\Desktop\New Folder\right.jpg',
                          wx.BITMAP_TYPE_ANY)
        bright.Rescale(100,100)
        bright2 = wx.StaticBitmap(self.panel, -1, wx.BitmapFromImage(bright),
                                  pos = (100,100))

As you can see, the code is exactly the same! I don't understand. The bleft works, but the bright give me invalid image, which to my understanding from what i've heard here means it can't find the image. My program is in the same folder as these images (New Folder). It no longer wants to add images from that folder. Help?

You are getting into trouble with escaped characters caused by \ since '\r' is read as CR (carriage return).
Try to use:

bright = wx.Image(r'C:\Users\Alexander\Desktop\New Folder\right.jpg', wx.BITMAP_TYPE_ANY)

or:

bright = wx.Image('C:\\Users\\Alexander\\Desktop\\New Folder\\right.jpg', wx.BITMAP_TYPE_ANY)

or:

bright = wx.Image('C:/Users/Alexander/Desktop/New Folder/right.jpg', wx.BITMAP_TYPE_ANY)

for all your Window paths.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.