Created
November 1, 2011 05:54
-
-
Save PierrickKoch/1330022 to your computer and use it in GitHub Desktop.
ROS image_view wxPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
usage: | |
rosrun proteus_demo ImageView.py image:=/ATRV/CameraMain | |
""" | |
import roslib | |
roslib.load_manifest('rospy') | |
roslib.load_manifest('sensor_msgs') | |
import rospy | |
from sensor_msgs.msg import Image | |
import wx | |
import sys | |
class ImageViewApp(wx.App): | |
def OnInit(self): | |
self.frame = wx.Frame(None, title = "ROS Image View", size = (256, 256)) | |
self.panel = ImageViewPanel(self.frame) | |
self.frame.Show(True) | |
return True | |
class ImageViewPanel(wx.Panel): | |
""" class ImageViewPanel creates a panel with an image on it, inherits wx.Panel """ | |
def update(self, image): | |
# http://www.ros.org/doc/api/sensor_msgs/html/msg/Image.html | |
if not hasattr(self, 'staticbmp'): | |
self.staticbmp = wx.StaticBitmap(self) | |
frame = self.GetParent() | |
frame.SetSize((image.width, image.height)) | |
if image.encoding == 'rgba8': | |
bmp = wx.BitmapFromBufferRGBA(image.width, image.height, image.data) | |
self.staticbmp.SetBitmap(bmp) | |
elif image.encoding == 'rgb8': | |
bmp = wx.BitmapFromBuffer(image.width, image.height, image.data) | |
self.staticbmp.SetBitmap(bmp) | |
def handle_image(image): | |
# make sure we update in the UI thread | |
wx.CallAfter(wx.GetApp().panel.update, image) | |
# http://wiki.wxpython.org/LongRunningTasks | |
def main(argv): | |
app = ImageViewApp() | |
rospy.init_node('ImageView') | |
rospy.Subscriber('/image', Image, handle_image) | |
print(__doc__) | |
app.MainLoop() | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment