Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Last active July 24, 2018 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatakamanbou/616a5497d0fc6ab0c217a8dabd46350a to your computer and use it in GitHub Desktop.
Save takatakamanbou/616a5497d0fc6ab0c217a8dabd46350a to your computer and use it in GitHub Desktop.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import wx
import numpy as np
import cv2
import sys
uni3 = cv2.imread('blackuni3.png')
class ImagePanel(wx.Panel):
def __init__(self, parent, size, ID = wx.ID_ANY):
wx.Panel.__init__(self, parent, ID, size = size)
self.size = size
bmp = wx.EmptyBitmap(self.size[0], self.size[1])
self.stbmp = wx.StaticBitmap(self, bitmap = bmp)
def redraw(self, img):
assert(img.ndim == 3)
assert(img.dtype == np.uint8)
assert(img.shape[0] == self.size[1] and img.shape[1] == self.size[0])
buf = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
bmp = wx.BitmapFromBuffer(img.shape[1], img.shape[0], buf)
self.stbmp.SetBitmap(bmp)
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title)
self.ip = ImagePanel(self, size = (uni3.shape[1], uni3.shape[0]))
self.button = wx.Button(self, wx.ID_ANY, 'Press Me')
self.button.Bind(wx.EVT_BUTTON, self.buttonPressed)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.ip, border = 10, flag = wx.ALIGN_CENTER | wx.ALL)
sizer.Add(self.button, border = 10, flag = wx.ALIGN_CENTER | wx.ALL)
self.SetSizerAndFit(sizer)
self.black = True
def buttonPressed(self, event = None):
img = uni3
if not self.black:
img = 255 - img
self.ip.redraw(img)
self.black = not self.black
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame(None, wx.ID_ANY, sys.argv[0])
frame.Show()
app.MainLoop()
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import wx
import wx.lib.newevent
import numpy as np
import cv2
import sys
import time
import threading
uni3 = cv2.imread('blackuni3.png')
class ImageSetter(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.img = uni3
self.size = (self.img.shape[1], self.img.shape[0])
def setTarget(self, target):
self.target = target
def run(self):
for i in range(10):
if i % 2 == 0:
img = self.img
else:
img = 255 - self.img
time.sleep(1)
#evt = ImageViewer.Event(img = img, msg = 'hoge %d' % i)
evt = self.target.__class__.Event(img = img, msg = 'hoge %d' % i)
wx.PostEvent(self.target, evt)
class ImageViewer(wx.Panel):
Event, EVT_NEWIMAGE = wx.lib.newevent.NewEvent()
def __init__(self, parent, setter, ID = wx.ID_ANY):
self.setter = setter
self.size = self.setter.size
wx.Panel.__init__(self, parent, ID, size = self.size)
self.stbmp = wx.StaticBitmap(self, bitmap = wx.EmptyBitmap(self.size[0], self.size[1]))
setter.setTarget(self)
self.Bind(ImageViewer.EVT_NEWIMAGE, self.newImageArrived)
def start(self):
self.setter.start()
def stop(self):
self.setter.stop()
def redraw(self, img):
assert(img.ndim == 3)
assert(img.dtype == np.uint8)
assert(img.shape[0] == self.size[1] and img.shape[1] == self.size[0])
buf = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
bmp = wx.BitmapFromBuffer(img.shape[1], img.shape[0], buf)
self.stbmp.SetBitmap(bmp)
def newImageArrived(self, event):
self.redraw(event.img)
print(event.msg)
if __name__ == '__main__':
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, sys.argv[0])
imgViewer = ImageViewer(frame, ImageSetter())
sizer = wx.GridSizer()
sizer.Add(imgViewer, flag = wx.ALIGN_CENTER | wx.ALL)
frame.SetSizerAndFit(sizer)
frame.Show()
imgViewer.start()
app.MainLoop()
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import wx
import sys
class MyPanel(wx.Panel):
def __init__(self, parent, ID = wx.ID_ANY):
wx.Panel.__init__(self, parent, ID)
bmp1 = wx.Bitmap('blackuni3.png')
bmp2 = wx.Bitmap('uni3d.png')
stbmp1 = wx.StaticBitmap(self, wx.ID_ANY, bmp1)
stbmp2 = wx.StaticBitmap(self, wx.ID_ANY, bmp2)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(stbmp1, border = 10, flag = wx.ALL)
sizer.Add(stbmp2, border = 10, flag = wx.ALL)
self.SetSizerAndFit(sizer)
if __name__ == '__main__':
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, sys.argv[0])
panel = MyPanel(frame)
sizer = wx.GridSizer()
sizer.Add(panel, flag = wx.ALIGN_CENTER)
frame.SetSizerAndFit(sizer)
frame.Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment