Large image cropped when opened with wxImage

Hi all,

I'm trying to visualize a very large image (approximately 63000 by
300) in a ScrolledWindow.
The thing is, the left and the right of the image are cropped and I
can't seem to do much about it.

Is there some kind of size limitation (I didn't find anything in the
documentation about that...)?

Here's a snippet of my class MyScrolledWindow:

sw = wx.ScrolledWindow(self)
bmp = wx.Image(r'figure2.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()
sbmp = wx.StaticBitmap(sw, -1, bmp)
sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /
20) + 1)

Any help/insight would be very appreciated =)

Regards.

Is that verbatim?
you’ve got a syntax error in your statement (missing parentheses after the 20,20)
sw.SetScrollbars(20,20,**(**bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /
20) + 1)

···

On Fri, Dec 10, 2010 at 8:23 AM, Dr Funk clement.chastagnol@gmail.com wrote:

Hi all,

I’m trying to visualize a very large image (approximately 63000 by

  1. in a ScrolledWindow.

The thing is, the left and the right of the image are cropped and I

can’t seem to do much about it.

Is there some kind of size limitation (I didn’t find anything in the

documentation about that…)?

Here’s a snippet of my class MyScrolledWindow:

sw = wx.ScrolledWindow(self)

bmp = wx.Image(r’figure2.png’,wx.BITMAP_TYPE_PNG).ConvertToBitmap()

sbmp = wx.StaticBitmap(sw, -1, bmp)

sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /

Any help/insight would be very appreciated =)

Regards.

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Which platform?

It's possible that there is a 16-bit value someplace in the native code that is rolling over, but I would expect that at closer to 65536, not 63000... On the other hand, the Grid huge table sample in the demo deals with a virtual size of about (800000, 190000) without problems, so it is probably something else. Have you checked that the virtual size of the scrolledwindow is >= the image's size?

The next thing I would look at is drawing the image yourself instead of using a wx.StaticBitmap. If it still has the problem then at least you would know that the problem is with the scrolled window and not the wx.StaticBitmap widget. You can optimize your drawing by pulling out the portion of the bitmap that is visible or is in the update region and only drawing that bit of it.

···

On 12/10/10 8:23 AM, Dr Funk wrote:

Hi all,

I'm trying to visualize a very large image (approximately 63000 by
300) in a ScrolledWindow.
The thing is, the left and the right of the image are cropped and I
can't seem to do much about it.

Is there some kind of size limitation (I didn't find anything in the
documentation about that...)?

Here's a snippet of my class MyScrolledWindow:

sw = wx.ScrolledWindow(self)
bmp = wx.Image(r'figure2.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()
sbmp = wx.StaticBitmap(sw, -1, bmp)
sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /
20) + 1)

Any help/insight would be very appreciated =)

--
Robin Dunn
Software Craftsman

Here is a small sample of drawing on a huge scrolled window. It works for me on at least windows and Mac.

import wx
print wx.version()

class MyScrolledWindow(wx.ScrolledWindow):
     def __init__(self, *args, **kw):
         wx.ScrolledWindow.__init__(self, *args, **kw)
         self.Bind(wx.EVT_PAINT, self.OnPaint)
         self.SetVirtualSize((63000, 300))
         self.SetScrollRate(20,20)

     def OnPaint(self, evt):
         dc = wx.PaintDC(self)
         self.PrepareDC(dc)

         dc.SetPen(wx.Pen('red'))
         dc.SetBrush(wx.Brush('red'))

         dc.DrawCircle(20,20, 10)
         w,h = self.GetVirtualSize()
         dc.DrawCircle(w/2, h/2, 10)
         dc.DrawCircle(w-20, h-20, 10)

app = wx.App(False)
frm = wx.Frame(None, title='Whopper')
sw = MyScrolledWindow(frm)
frm.Show()
app.MainLoop()

···

On 12/10/10 11:06 AM, Robin Dunn wrote:

On 12/10/10 8:23 AM, Dr Funk wrote:

Hi all,

I'm trying to visualize a very large image (approximately 63000 by
300) in a ScrolledWindow.
The thing is, the left and the right of the image are cropped and I
can't seem to do much about it.

Is there some kind of size limitation (I didn't find anything in the
documentation about that...)?

Here's a snippet of my class MyScrolledWindow:

sw = wx.ScrolledWindow(self)
bmp = wx.Image(r'figure2.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()
sbmp = wx.StaticBitmap(sw, -1, bmp)
sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /
20) + 1)

Any help/insight would be very appreciated =)

Which platform?

It's possible that there is a 16-bit value someplace in the native code
that is rolling over, but I would expect that at closer to 65536, not
63000... On the other hand, the Grid huge table sample in the demo deals
with a virtual size of about (800000, 190000) without problems, so it is
probably something else. Have you checked that the virtual size of the
scrolledwindow is >= the image's size?

The next thing I would look at is drawing the image yourself instead of
using a wx.StaticBitmap. If it still has the problem then at least you
would know that the problem is with the scrolled window and not the
wx.StaticBitmap widget. You can optimize your drawing by pulling out the
portion of the bitmap that is visible or is in the update region and
only drawing that bit of it.

--
Robin Dunn
Software Craftsman

The attached modded demo shows 9 balls in tic-tac-toe grid positions.

Ray

Whopper.py (2.22 KB)

···

On Fri, Dec 10, 2010 at 2:17 PM, Robin Dunn robin@alldunn.com wrote:

On 12/10/10 11:06 AM, Robin Dunn wrote:

On 12/10/10 8:23 AM, Dr Funk wrote:

Hi all,

I’m trying to visualize a very large image (approximately 63000 by
300) in a ScrolledWindow.

The thing is, the left and the right of the image are cropped and I
can’t seem to do much about it.

Is there some kind of size limitation (I didn’t find anything in the
documentation about that…)?

Here’s a snippet of my class MyScrolledWindow:

sw = wx.ScrolledWindow(self)
bmp = wx.Image(r’figure2.png’,wx.BITMAP_TYPE_PNG).ConvertToBitmap()
sbmp = wx.StaticBitmap(sw, -1, bmp)
sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /

Any help/insight would be very appreciated =)

Which platform?

It’s possible that there is a 16-bit value someplace in the native code
that is rolling over, but I would expect that at closer to 65536, not

63000… On the other hand, the Grid huge table sample in the demo deals
with a virtual size of about (800000, 190000) without problems, so it is
probably something else. Have you checked that the virtual size of the

scrolledwindow is >= the image’s size?

The next thing I would look at is drawing the image yourself instead of
using a wx.StaticBitmap. If it still has the problem then at least you
would know that the problem is with the scrolled window and not the

wx.StaticBitmap widget. You can optimize your drawing by pulling out the
portion of the bitmap that is visible or is in the update region and
only drawing that bit of it.

Here is a small sample of drawing on a huge scrolled window. It works for me on at least windows and Mac.

import wx
print wx.version()

class MyScrolledWindow(wx.ScrolledWindow):
def init(self, *args, **kw):
wx.ScrolledWindow.init(self, *args, **kw)
self.Bind(wx.EVT_PAINT, self.OnPaint)

   self.SetVirtualSize((63000, 300))
   self.SetScrollRate(20,20)

def OnPaint(self, evt):
dc = wx.PaintDC(self)
self.PrepareDC(dc)

   dc.SetPen(wx.Pen('red'))

   dc.SetBrush(wx.Brush('red'))

   dc.DrawCircle(20,20, 10)
   w,h = self.GetVirtualSize()
   dc.DrawCircle(w/2, h/2, 10)
   dc.DrawCircle(w-20, h-20, 10)

app = wx.App(False)

frm = wx.Frame(None, title=‘Whopper’)
sw = MyScrolledWindow(frm)
frm.Show()
app.MainLoop()


Robin Dunn
Software Craftsman
http://wxPython.org

No, I pasted bmp.GetWidth() / 20) + 1, I had a hard-coded value to
test things before =)

···

On Dec 10, 7:27 pm, mw <m...@tangsoo.us> wrote:

Is that verbatim?

you've got a syntax error in your statement (missing parentheses after the
20,20)

sw.SetScrollbars(20,20,*(*bmp.GetWidth() / 20) + 1, (bmp.GetHeight() / 20) +
1)

On Fri, Dec 10, 2010 at 8:23 AM, Dr Funk <clement.chastag...@gmail.com>wrote:

> Hi all,

> I'm trying to visualize a very large image (approximately 63000 by
> 300) in a ScrolledWindow.
> The thing is, the left and the right of the image are cropped and I
> can't seem to do much about it.

> Is there some kind of size limitation (I didn't find anything in the
> documentation about that...)?

> Here's a snippet of my class MyScrolledWindow:

> sw = wx.ScrolledWindow(self)
> bmp = wx.Image(r'figure2.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()
> sbmp = wx.StaticBitmap(sw, -1, bmp)
> sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /
> 20) + 1)

> Any help/insight would be very appreciated =)

> Regards.

> --
> To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> or visithttp://groups.google.com/group/wxPython-users?hl=en

Hi all!
Thanks a lot for your very fast answers!! =)

I have solved my problem, well, rather got around it.
I used PIL to chop the image in smaller chunks, loaded them as
StaticBitmap in several Panel and then displayed them in a
ScrolledWindow using a horizontal BoxSizer.

Below are code samples performing these operations, if anybody ever
encounters a similar problem.

@Robin :

Which platform?

Windows 7 on a 64 bits machine

Have you checked that the virtual size of the scrolledwindow is >= the image's size?

Yes it is. The image size is 550Ko whereas the virtual size is 49Mo
(as stated when opened with IrfanView).

The next thing I would look at is drawing the image yourself instead of using a wx.StaticBitmap.

In fact, the image is generated with Matplotlib. I saw that it's
possible to use wxPython as a backend for Matplotlib (not built-in,
but some people managed to do it), but simply it felt easier to export
the image as a file and then open it with wxPython.

##### code samples ####

## slicing images ##
from PIL import Image

im = Image.open(r'figure2.jpg')
number_of_parts = 4

width, height = im.size
for i in range(number_of_parts) :
    outfile = r'figure2-' + str(i)
    region = im.crop((i*width/number_of_parts,
                      0,
                      (i+1)*width/number_of_parts,
                      height))
    region.save(outfile, "JPEG")

## displaying the images ##
import wx

class ScrolledWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(500, 400))

        sw = wx.ScrolledWindow(self)
        box = wx.BoxSizer(wx.HORIZONTAL)

        bmp0 =
wx.Image(r'figure2-0.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        bmp1 =
wx.Image(r'figure2-1.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        bmp2 =
wx.Image(r'figure2-2.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        bmp3 =
wx.Image(r'figure2-3.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()

        panel0 = wx.Panel(sw)
        panel1 = wx.Panel(sw)
        panel2 = wx.Panel(sw)
        panel3 = wx.Panel(sw)

        sbmp0 = wx.StaticBitmap(panel0, -1, bmp0)
        sbmp1 = wx.StaticBitmap(panel1, -1, bmp1)
        sbmp2 = wx.StaticBitmap(panel2, -1, bmp2)
        sbmp3 = wx.StaticBitmap(panel3, -1, bmp3)

        box.Add(panel0, 0, wx.ALIGN_LEFT|wx.ALL, 0)
        box.Add(panel1, 0, wx.ALIGN_LEFT|wx.ALL, 0)
        box.Add(panel2, 0, wx.ALIGN_LEFT|wx.ALL, 0)
        box.Add(panel3, 0, wx.ALIGN_LEFT|wx.ALL, 0)

        sw.SetSizer(box)
        scroll_step = 500
        sw.SetScrollbars(scroll_step, scroll_step, (3 *
bmp0.GetWidth() / scroll_step) + 1, (bmp0.GetHeight() / scroll_step) +
1)

        #self.SetAutoLayout(1)
        self.Centre()
        self.Show()

app = wx.App()
ScrolledWindow(None, -1, 'Test')
app.MainLoop()

···

On Dec 13, 11:13 am, Dr Funk <clement.chastag...@gmail.com> wrote:

No, I pasted bmp.GetWidth() / 20) + 1, I had a hard-coded value to
test things before =)

On Dec 10, 7:27 pm, mw <m...@tangsoo.us> wrote:

> Is that verbatim?

> you've got a syntax error in your statement (missing parentheses after the
> 20,20)

> sw.SetScrollbars(20,20,*(*bmp.GetWidth() / 20) + 1, (bmp.GetHeight() / 20) +
> 1)

> On Fri, Dec 10, 2010 at 8:23 AM, Dr Funk <clement.chastag...@gmail.com>wrote:

> > Hi all,

> > I'm trying to visualize a very large image (approximately 63000 by
> > 300) in a ScrolledWindow.
> > The thing is, the left and the right of the image are cropped and I
> > can't seem to do much about it.

> > Is there some kind of size limitation (I didn't find anything in the
> > documentation about that...)?

> > Here's a snippet of my class MyScrolledWindow:

> > sw = wx.ScrolledWindow(self)
> > bmp = wx.Image(r'figure2.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()
> > sbmp = wx.StaticBitmap(sw, -1, bmp)
> > sw.SetScrollbars(20, 20, bmp.GetWidth() / 20) + 1, (bmp.GetHeight() /
> > 20) + 1)

> > Any help/insight would be very appreciated =)

> > Regards.

> > --
> > To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com<wxPython-users%2Bunsubscribe@googlegroups.com>
> > or visithttp://groups.google.com/group/wxPython-users?hl=en

I have solved my problem, well, rather got around it.
I used PIL to chop the image in smaller chunks, loaded them as
StaticBitmap in several Panel and then displayed them in a
ScrolledWindow using a horizontal BoxSizer.

There is nothing like working code, but...

That's a pretty complicated way to go about it -- it feels fragile to me, though I can't say I know why it wouldn't work everywhere.

The next thing I would look at is drawing the image yourself instead of using a wx.StaticBitmap.

In fact, the image is generated with Matplotlib.

Robin's example may have misled you, but when he write "draw it yourself", he didn't mean draw the picture yourself. You c an draw a Bitmap with DC.DrawBitmap. That's the simplest way to go if all you need to do is draw a bitmap in a Window. None of the overhead of a StaticBitmap, so less likely to suffer bugs like you've seen.

I saw that it's
possible to use wxPython as a backend for Matplotlib (not built-in,
but some people managed to do it),

not "some people managed" -- wx is a well supported, commonly used back-end. This all would be a lot simpler if you used it that way.

Take a look at the "embedded in wx" examples, or, even easier, wxMPL:

http://agni.phys.iit.edu/~kmcivor/wxmpl/

but simply it felt easier to export
the image as a file and then open it with wxPython.

but poor performance and no interactivity -- maybe still fine for your use. But even if you stick with this route, do consider just drawing the bitmap to the Window with a DC. Start with Robin's example, and replace the Draw Calls with a DrawBitmap -- it should be that simple.

-Chris

···

On 12/13/10 5:36 AM, Dr Funk wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

FWIW, wxMPL is clearly stated not to have been tested on MSW
platforms, but its demos work fine on :

Windows 6.1.7600
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
(Intel)]
Wx 2.8.11.0

Ray Pasco

···

On Dec 13, 3:33 pm, Christopher Barker <Chris.Bar...@noaa.gov> wrote:

On 12/13/10 5:36 AM, Dr Funk wrote:

> I saw that it's
> possible to use wxPython as a backend for Matplotlib (not built-in,
> but some people managed to do it),

not "some people managed" -- wx is a well supported, commonly used
back-end. This all would be a lot simpler if you used it that way.

Take a look at the "embedded in wx" examples, or, even easier, wxMPL:

but poor performance and no interactivity -- maybe still fine for your
use. But even if you stick with this route, do consider just drawing the
bitmap to the Window with a DC. Start with Robin's example, and replace
the Draw Calls with a DrawBitmap -- it should be that simple.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Bar...@noaa.gov

hmm -- I never noticed that, nor have I had a problem with my (fairly small) use on Windows.

-Chris

···

On 12/14/10 10:14 AM, WinCrazy wrote:

FWIW, wxMPL is clearly stated not to have been tested on MSW
platforms,

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov