Couldn't add an image to the wx.ImageList for use in wxListBook

All,

I can't figure this out. I have a wxListBook control to display
images. I have thumbnails in the ListCtrl and the full size image on
the page.

Thumbnail images are added to the wxImageList without problem UNLESS
the image is portrait, ie, greater height than width, then I get the
message "Couldn't add an image to the image list" and a -1 returned
from the il.Add(bmp). However, if I click in the ListCtrl where the
thumbnail should be, I can view the corresponding image.

I'm sure its something to do with masks, on which I am a bit of an
ignoramus.

Here is the code:

from PIL import Image

   def set_images(self):
        Sz = 50
        il = wx.ImageList(Sz,Sz)

        path = <image path>

        imOrig = Image.open(path)
        imThumb = imOrig.copy()
        imThumb.thumbnail((Sz,Sz))
        bmp = self.PilImageToWxBitmap(imThumb)

        #- Add the thumb to the list
        res = il.Add(bmp)

        # Now make the panels for the list book
        ...

       self.listbook.AssignImageList(il)

    def PilImageToWxBitmap(self, myPilImage ) :
        return
self.WxImageToWxBitmap( self.PilImageToWxImage( myPilImage ) )

    def WxImageToWxBitmap(self, myWxImage ) :
        return myWxImage.ConvertToBitmap()

    def PilImageToWxImage(self, myPilImage ):
        myWxImage = wx.EmptyImage( myPilImage.size[0],
myPilImage.size[1] )
        myWxImage.SetData( myPilImage.convert( 'RGB' ).tostring() )
        return myWxImage

regards
Phil

Please make a runnable, small as possible, sample application that demonstrates the problem, include one of the images that is giving you trouble, and let us know the platform and wx version. MakingSampleApps - wxPyWiki

···

On 6/15/11 9:13 AM, puppriss wrote:

All,

I can't figure this out. I have a wxListBook control to display
images. I have thumbnails in the ListCtrl and the full size image on
the page.

Thumbnail images are added to the wxImageList without problem UNLESS
the image is portrait, ie, greater height than width, then I get the
message "Couldn't add an image to the image list" and a -1 returned
from the il.Add(bmp). However, if I click in the ListCtrl where the
thumbnail should be, I can view the corresponding image.

I'm sure its something to do with masks, on which I am a bit of an
ignoramus.

Here is the code:

--
Robin Dunn
Software Craftsman

Hi Robin,

Here's the app. The image is just a simple black & white image in
portrait. There doesn't seem to be an option to attach it to the post,
but I believe any portrait image will fail.

Cheers
Phil

import wx
from PIL import Image

def create(parent):
    return Frame1(parent)

class Frame1(wx.Frame):
    def ctrls(self, prnt):
        wx.Frame.__init__(self, prnt, wx.ID_ANY,
style=wx.DEFAULT_FRAME_STYLE,
            pos=wx.Point(-1, -1), size=wx.Size(500, 500))
        self.panMain = wx.Panel(self, wx.ID_ANY)
        self.topbook = wx.Listbook(self.panMain, id=wx.ID_ANY,
size=wx.Size(500,500))
        self.bsMain = wx.BoxSizer(orient=wx.VERTICAL)
        self.bsMain.Add(self.topbook, 1, wx.EXPAND | wx.ALL, 5)
        self.panMain.SetSizer(self.bsMain)
        self.panMain.Layout()

    def __init__(self, parent):
        self.ctrls(parent)
        self.set_images()

    def set_images(self):
        Sz = 50
        il = wx.ImageList(Sz,Sz)
        path = "C:\\Users\\file.bmp"
        imOrig = Image.open(path)
        imThumb = imOrig.copy()
        imThumb.thumbnail((Sz,Sz))
        bmp = self.PilImageToWxBitmap(imThumb)
        res = il.Add(bmp)
        win = wx.Panel(self.topbook, -1, style=wx.SUNKEN_BORDER)
        self.topbook.AddPage(win, '', imageId=0)
        imOrig.thumbnail((400,400),Image.ANTIALIAS)
        bmp = self.PilImageToWxBitmap(imOrig)
        im = wx.StaticBitmap(win, -1, bmp, style=wx.BORDER_SIMPLE)
        win.Layout()
        self.topbook.AssignImageList(il)

    def PilImageToWxBitmap(self, myPilImage ) :
        return
self.WxImageToWxBitmap( self.PilImageToWxImage( myPilImage ) )

    def WxImageToWxBitmap(self, myWxImage ) :
        return myWxImage.ConvertToBitmap()

    def PilImageToWxImage(self, myPilImage ):
        myWxImage = wx.EmptyImage( myPilImage.size[0],
myPilImage.size[1] )
        myWxImage.SetData( myPilImage.convert( 'RGB' ).tostring() )
        return myWxImage

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

···

On Jun 15, 8:23 pm, Robin Dunn <ro...@alldunn.com> wrote:

On 6/15/11 9:13 AM, puppriss wrote:

> All,

> I can't figure this out. I have a wxListBook control to display
> images. I have thumbnails in the ListCtrl and the full size image on
> the page.

> Thumbnail images are added to the wxImageList without problem UNLESS
> the image is portrait, ie, greater height than width, then I get the
> message "Couldn't add an image to the image list" and a -1 returned
> from the il.Add(bmp). However, if I click in the ListCtrl where the
> thumbnail should be, I can view the corresponding image.

> I'm sure its something to do with masks, on which I am a bit of an
> ignoramus.

> Here is the code:

Please make a runnable, small as possible, sample application that
demonstrates the problem, include one of the images that is giving you
trouble, and let us know the platform and wx version.MakingSampleApps - wxPyWiki

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

Hi Robin,

Here's the app. The image is just a simple black& white image in
portrait. There doesn't seem to be an option to attach it to the post,

You didn't read #7 at MakingSampleApps - wxPyWiki very well, did you? :wink:

but I believe any portrait image will fail.

Apparently PIL's thumbnail method is going to maintain the aspect ratio in at least some cases, and so you can end up with an image that is not (50,50). One of my testcases made a thumbnail that was (37,50). wx.ImageList requires that all the icons it holds are the same size.

BTW, you can also use wx.Image instead of PIL for simple image manipulations like this, and wx.Image.Scale will always give you an image of the specified size.

···

On 6/16/11 1:32 AM, puppriss wrote:

--
Robin Dunn
Software Craftsman

Robin,

You didn’t read #7 at http://wiki.wxpython.org/MakingSampleApps very
well, did you? :wink:
Not only did I not read it well, I didn’t read it at all! But now I know.
Apparently PIL’s thumbnail method is going to maintain the aspect ratio
It does indeed maintain the aspect ratio. wx.ImageList accepts a (50,37) thumbnail, for example, but not a (37,50) for some reason! In the case of the former, the bottom part of the image is padded with black to make it 50. I don’t know why it doesn’t do the same to the right side of the image in the case of a portrait thumbnail.

Oh well, I will use wx.Image.Scale. The thumbnails are stretched but I can live with that.

Thanks for your help
Phil

You could probably start off with a black 50x50 image, then scale your
image within the 50x50 bounding box. Finally overlay the resized image
onto the black image to produce the 50x50 image you need (with black
stripes at top/bottom, right/left).

It seems like (from your description) this happens for landscape
pictures, but not portrait, if you handle this yourself, it will be
done in either case.

···

On Fri, Jun 17, 2011 at 10:42 AM, puppriss <buttsy@gmail.com> wrote:

Oh well, I will use wx.Image.Scale. The thumbnails are stretched but I can
live with that.