I am trying to develop an application using wxwidgets where i can load an image and paint on top of the image - basically like the Whyteboard app: http://code.google.com/p/whyteboard/
However, unlike whyteboard i need to be able to save the drawing in its own image so that i have a png file with the drawing of the user (this is actually a mask for the image)
So what i am trying to create is an annotation tool where user can load an image, draw a freehand shape and save the image of the freehandshape.
I would suspect this needs some sort of layering of panels, but i have not been able to find out how i can do this.
The code i have so far i seen below. What it does is that it creates an artificial image from a numpy array and displays it.
Also it captures mouse events so that it is possible to draw - The thing is that the mousecapture does not work when the image is loaded…it only works if i disable the part of the code enapsulated in ################################.
How do i change this so that i can drop on a transparent panel instead and store this panel as an image ?
I really hope for help on this one.
Thanks a lot.
import sys, os
import wx
import wx.lib.scrolledpanel as scrolled
import numpy as np
class ImgPanel(scrolled.ScrolledPanel):
def __init__(self, parent):
super(ImgPanel, self).__init__(parent, style = wx.SUNKEN_BORDER)
self.bitmap=wx.StaticBitmap(parent=self)
#image = wx.Bitmap('/home/ziebel/Desktop/G20mega.png')
self.bitmap=wx.StaticBitmap(parent=self)
array = np.zeros( (100, 100, 3),'uint8')
array[:,:,] = (255,127,0)
image = wx.EmptyImage(100,100)
image.SetData( array.tostring())
wxBitmap = image.ConvertToBitmap()
<details class='elided'>
<summary title='Show trimmed content'>···</summary>
################################
self.bitmap.SetBitmap(wxBitmap)
self.imgSizer = wx.BoxSizer(wx.VERTICAL) self.imgSizer.Add(self.bitmap, 1, wx.EXPAND)
self.SetSizer(self.imgSizer)
self.SetAutoLayout(1)
self.SetupScrolling() self.IsRectReady = False
self.newRectPara=[0,0,0,0]
################################
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.leftdown=False
prevpt = None def OnMotion(self, event=None):
if self.leftdown:
if self.prevpt == None:
self.prevpt = (event.X, event.Y)
dc = wx.PaintDC(self)
dc.DrawLine(self.prevpt[0],self.prevpt[1],event.X, event.Y)
self.prevpt = (event.X, event.Y)
def OnLeftDown(self, event=None):
self.leftdown=True
def OnLeftUp(self, event=None):
self.leftdown=False
self.prevpt = None
def OnPaint(self, event=None):
dc = wx.PaintDC(self)
dc.Clear()
class DrawPanel(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.imgPanel = ImgPanel(self)
class App(wx.App):
def OnInit(self):
frame = DrawPanel(None, -1, "wxBitmap Test", wx.DefaultPosition,(550,200))
self.SetTopWindow(frame)
frame.Show(True)
return True
if __name__ == "__main__":
app = App(0)
app.MainLoop()