Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 from PIL import Image
00034
00035
00036 import sys
00037 from python_qt_binding import QT_BINDING_MODULES
00038 if (
00039 not QT_BINDING_MODULES['QtCore'].__name__.startswith('PyQt5') and
00040 'PyQt5' in sys.modules
00041 ):
00042 sys.modules['PyQt5'] = None
00043 from PIL.ImageQt import ImageQt
00044
00045 from rqt_bag import TopicMessageView
00046 from rqt_bag_plugins import image_helper
00047
00048 from python_qt_binding.QtGui import QPixmap
00049 from python_qt_binding.QtWidgets import QGraphicsScene, QGraphicsView
00050
00051
00052 class ImageView(TopicMessageView):
00053
00054 """
00055 Popup image viewer
00056 """
00057 name = 'Image'
00058
00059 def __init__(self, timeline, parent, topic):
00060 super(ImageView, self).__init__(timeline, parent, topic)
00061
00062 self._image = None
00063 self._image_topic = None
00064 self._image_stamp = None
00065 self.quality = Image.NEAREST
00066
00067
00068 self._overlay_font_size = 14.0
00069 self._overlay_indent = (4, 4)
00070 self._overlay_color = (0.2, 0.2, 1.0)
00071
00072 self._image_view = QGraphicsView(parent)
00073 self._image_view.resizeEvent = self._resizeEvent
00074 self._scene = QGraphicsScene()
00075 self._image_view.setScene(self._scene)
00076 parent.layout().addWidget(self._image_view)
00077
00078
00079 def _resizeEvent(self, event):
00080
00081
00082 self._scene.setSceneRect(
00083 0, 0, self._image_view.size().width() - 2, self._image_view.size().height() - 2)
00084 self.put_image_into_scene()
00085
00086 def message_viewed(self, bag, msg_details):
00087 """
00088 refreshes the image
00089 """
00090 TopicMessageView.message_viewed(self, bag, msg_details)
00091 topic, msg, t = msg_details[:3]
00092 if not msg:
00093 self.set_image(None, topic, 'no message')
00094 else:
00095 self.set_image(msg, topic, msg.header.stamp)
00096
00097 def message_cleared(self):
00098 TopicMessageView.message_cleared(self)
00099 self.set_image(None, None, None)
00100
00101
00102 def put_image_into_scene(self):
00103 if self._image:
00104 resized_image = self._image.resize(
00105 (self._image_view.size().width() - 2, self._image_view.size().height() - 2), self.quality)
00106
00107 QtImage = ImageQt(resized_image)
00108 pixmap = QPixmap.fromImage(QtImage)
00109 self._scene.clear()
00110 self._scene.addPixmap(pixmap)
00111
00112 def set_image(self, image_msg, image_topic, image_stamp):
00113 self._image_msg = image_msg
00114 if image_msg:
00115 self._image = image_helper.imgmsg_to_pil(image_msg)
00116 else:
00117 self._image = None
00118 self._image_topic = image_topic
00119 self._image_stamp = image_stamp
00120 self.put_image_into_scene()