wx_drawer.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = "heider"
  4. __doc__ = r"""
  5. Created on 9/9/22
  6. """
  7. __all__ = []
  8. def uahsduiasdj():
  9. """
  10. :return:
  11. :rtype:
  12. """
  13. import wx # pip install -U wxPython
  14. from PIL import Image
  15. SIZE = (640, 480)
  16. def get_image():
  17. """
  18. :return:
  19. :rtype:
  20. """
  21. # Put your code here to return a PIL image from the camera.
  22. return Image.new("L", SIZE)
  23. def pil_to_wx(image):
  24. """
  25. :param image:
  26. :type image:
  27. :return:
  28. :rtype:
  29. """
  30. width, height = image.size
  31. buffer = image.convert("RGB").tostring()
  32. bitmap = wx.BitmapFromBuffer(width, height, buffer)
  33. return bitmap
  34. class Panel(wx.Panel):
  35. """ """
  36. def __init__(self, parent):
  37. super(Panel, self).__init__(parent, -1)
  38. self.SetSize(SIZE)
  39. self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
  40. self.Bind(wx.EVT_PAINT, self.on_paint)
  41. self.update()
  42. def update(self):
  43. """ """
  44. self.Refresh()
  45. self.Update()
  46. wx.CallLater(15, self.update)
  47. def create_bitmap(self):
  48. """
  49. :return:
  50. :rtype:
  51. """
  52. image = get_image()
  53. bitmap = pil_to_wx(image)
  54. return bitmap
  55. def on_paint(self, event):
  56. """
  57. :param event:
  58. :type event:
  59. """
  60. bitmap = self.create_bitmap()
  61. dc = wx.AutoBufferedPaintDC(self)
  62. dc.DrawBitmap(bitmap, 0, 0)
  63. class Frame(wx.Frame):
  64. """ """
  65. def __init__(self):
  66. style = wx.DEFAULT_FRAME_STYLE & ~wx.RESIZE_BORDER & ~wx.MAXIMIZE_BOX
  67. super(Frame, self).__init__(None, -1, "Camera Viewer", style=style)
  68. panel = Panel(self)
  69. self.Fit()
  70. app = wx.PySimpleApp()
  71. frame = Frame()
  72. frame.Center()
  73. frame.Show()
  74. app.MainLoop()
  75. if __name__ == "__main__":
  76. uahsduiasdj()