Hope you kind folk can spare a bit of advice. I'm trying to find a clever way to modifying a part of an image without replacing the image completely. For instance, let's say I have an image of a car and I would like to show the tire is flat. Instead of replacing the image completely with another, I would like to layer a new image of a flat tire image on top of current tire image. Eventually I would like to extend this to show multiple stats of the car, flat tires, ajar doors, headlight burnt out, etc etc. Since each of these stats can occur independently, just replacing one entire image for each combination of stats is crazy inefficient.
I've been using a bit of code that layers a bitmap (with a transparent background) on top another. What I'm not sure about is how to modify the layered images. Let's say I have 5 smaller images layered on top of the large car image, is there a way I can change the bitmap of one of the layered images in real time and delete those I no longer wish to display?
In the below code, I draw the "low_tire.png" image on top the "car.png". Then when the button is clicked I would like the "low_tire.png" to be changed with the "flat_tire.png" image. How would I accomplish this? And how would I delete the layered image completely?
Here's my code thus far:
import wx class Frame1(wx.Frame): def __init__(self, prnt): wx.Frame.__init__(self, id=-1, name='', parent=prnt, pos=wx.Point(0, 0), size=wx.Size(600, 600), style=wx.DEFAULT_FRAME_STYLE, title='Frame1') self.panel1 = BmpPanel(self) self.Show() class BmpPanel(wx.Panel): def __init__(self, parent, id = -1): wx.Panel.__init__(self, parent, id) self.Bind(wx.EVT_PAINT, self.OnPaint) self.button = wx.Button(self, id, 'click to change layered image') self.Bind(wx.EVT_BUTTON, self.modTheImage, id=self.button.GetId()) self.BackBmp = wx.Bitmap('car.png') self.FrontBmp = wx.Bitmap('low_tire.png') self.hiBmp = wx.Bitmap('door_ajar.png') def OnPaint(self, event): self.dc = wx.PaintDC(self) self.Update() def Update(self): self.dc.DrawBitmap(self.BackBmp, 96, 72, True) self.dc.DrawBitmap(self.FrontBmp, 150, 110, True) def modTheImage(self, event): self.FrontBmp = wx.Bitmap('flat_tire.png') # ATTEMPT TO CHANCE IMAGE FILE (DOES NOT WORK) #self.Update #self.Refresh() # USING THIS SEEMS TO CRASH THE APPLICATION App = wx.PySimpleApp() application = Frame1(None) application.Show() App.MainLoop()