Hi Ryan,
On 5/21/07, Ryan Krauss wrote:
> How would I go about writing the OnPaint method?
Try this piece of code, taken from the InfinImage implementation (it
requires PIL, Python Image Library).
import wx
import Image
def BitmapToPil(bitmap):
return ImageToPil(BitmapToImage(bitmap))
def BitmapToImage(bitmap):
return wx.ImageFromBitmap(bitmap)
def ImageToBitmap(image):
return image.ConvertToBitmap()
def PilToBitmap(pil):
return ImageToBitmap(PilToImage(pil))
def PilToImage(pil):
image = wx.EmptyImage(pil.size[0], pil.size[1])
image.SetData(pil.convert('RGB').tostring())
return image
def ImageToPil(image):
pil = Image.new('RGB',(image.GetWidth(), image.GetHeight()))
pil.fromstring(image.GetData())
return pil
class ScrolledImage(wx.ScrolledWindow):
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize):
wx.ScrolledWindow.__init__(self, parent, id, pos, size)
self.size = wx.Size(0, 0)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def ShowImage(self, image):
PIL_bitmap = Image.open(image)
bmp = PilToBitmap(PIL_bitmap)
self.SetScrollbars(1, 1, PIL_bitmap.size[0],
PIL_bitmap.size[1], 0, 0)
size = self.GetSize()
xmin = (size.x - PIL_bitmap.size[0])/2
ymin = (size.y - PIL_bitmap.size[1])/2
self.currentbmp = bmp
self.size = wx.Size(self.currentbmp.GetSize().width,
self.currentbmp.GetSize().height)
self.Refresh()
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.Clear()
self.DoPaint(dc)
def DoPaint(self):
self.PrepareDC(dc)
iw, ih = self.size
xmin = (self.windowsize.x - iw)/2
ymin = (self.windowsize.y - ih)/2
bm_dc = wx.MemoryDC()
bm_dc.SelectObject(self.currentbmp)
if xmin < 0:
xmin = 0
if ymin < 0:
ymin = 0
dc.Blit(xmin, ymin, iw, ih, bm_dc, 0, 0, wx.COPY, True)
bm_dc.SelectObject(wx.NullBitmap)
def OnEraseBackground(self, event):
pass
def OnSize(self, event):
iw, ih = self.size.width, self.size.height
w, h = event.GetSize()
scroll_x = w < iw
scroll_y = h < ih
scroll = scroll_x | scroll_y
x, y = self.GetViewStart()
self.SetScrollbars(scroll,scroll,iw,ih,x,y)
self.windowsize = wx.Size(w, h)
>
> On 5/21/07, Andrea Gavana <andrea.gavana@gmail.com> wrote:
> > HI Ryan,
> >
> > On 5/21/07, Ryan Krauss wrote:
> > > I am confused about displaying images in wxPython. I have looked
> > > through the archive and see other people with related problems, but
> > > don't see an answer to my specific one. I am trying to create an
> > > application for associating captions with pictures. I am using Andrea
> > > Gavana's ThumbCtrl to display thumbnails and when I click on a
> > > thumbnail, I want to display it at a larger size and have a textctrl
> > > for adding a caption. The captions and there associated filename
> > > might then be stored in some database or xml file and be used to
> > > generate some html code for a photoalbum or something. I originally
> > > thought that my larger picture would just be a StaticBitmap, but I
> > > have two problems. First, there doesn't seem to be a way to change
> > > the picture for StaticBitmap once it has been created. I need to be
> > > able to load many different images one at a time into this control.
> > > Second, there is a caution about the size of StaticBitmaps. Is this
> > > still a concern? What control should I use and what methods?
> >
> > I wouldn't use a StaticBitmap for that, but a wx.ScrolledWindow in which
I
> > would define my own OnPaint method to draw the selected image. This was
my
> > approach when I first wrote a small piece of software called
"InfinImage",
> > based on ThumbnailCtrl and the aforementioned approach, with zooming,
> > panning and other whistles and bells. I called for some volunteers in
the
> > development of InfinImage on this list, but at that time there wasn't
much
> > interest. In my view, it would have become a competitor of Cornice (and
> > maybe more). However, my suggestion would be to draw the image yourself:
you
> > will have much more control on it than with a normal StaticBitmap (or
> > wx.lib.statbmp equivalent).
> >
> > Andrea.
> >
> > "Imagination Is The Only Weapon In The War Against Reality."
> > http://xoomer.virgilio.it/infinity77/
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
> For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org
>
--
Andrea.
"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/