image_view.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from PIL import Image
00034 
00035 # HACK workaround for upstream pillow issue python-pillow/Pillow#400
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     Popup image viewer
00055     """
00056     name = 'Image'
00057 
00058     def __init__(self, timeline, parent, topic):
00059         super(ImageView, self).__init__(timeline, parent, topic)
00060 
00061         self._image = None
00062         self._image_topic = None
00063         self._image_stamp = None
00064         self.quality = Image.NEAREST  # quality hint for scaling
00065 
00066         # TODO put the image_topic and image_stamp on the picture or display them in some fashion
00067         self._overlay_font_size = 14.0
00068         self._overlay_indent = (4, 4)
00069         self._overlay_color = (0.2, 0.2, 1.0)
00070 
00071         self._image_view = QGraphicsView(parent)
00072         self._image_view.resizeEvent = self._resizeEvent
00073         self._scene = QGraphicsScene()
00074         self._image_view.setScene(self._scene)
00075         parent.layout().addWidget(self._image_view)
00076 
00077     # MessageView implementation
00078     def _resizeEvent(self, event):
00079         # TODO make this smarter. currently there will be no scrollbar even if the timeline extends beyond the viewable area
00080         self._scene.setSceneRect(0, 0, self._image_view.size().width() - 2, self._image_view.size().height() - 2)
00081         self.put_image_into_scene()
00082 
00083     def message_viewed(self, bag, msg_details):
00084         """
00085         refreshes the image
00086         """
00087         TopicMessageView.message_viewed(self, bag, msg_details)
00088         topic, msg, t = msg_details[:3]
00089         if not msg:
00090             self.set_image(None, topic, 'no message')
00091         else:
00092             self.set_image(msg, topic, msg.header.stamp)
00093 
00094     def message_cleared(self):
00095         TopicMessageView.message_cleared(self)
00096         self.set_image(None, None, None)
00097 
00098     # End MessageView implementation
00099     def put_image_into_scene(self):
00100         if self._image:
00101             resized_image = self._image.resize((self._image_view.size().width() - 2, self._image_view.size().height() - 2), self.quality)
00102 
00103             QtImage = ImageQt(resized_image)
00104             pixmap = QPixmap.fromImage(QtImage)
00105             self._scene.clear()
00106             self._scene.addPixmap(pixmap)
00107 
00108     def set_image(self, image_msg, image_topic, image_stamp):
00109         self._image_msg = image_msg
00110         if image_msg:
00111             self._image = image_helper.imgmsg_to_pil(image_msg)
00112         else:
00113             self._image = None
00114         self._image_topic = image_topic
00115         self._image_stamp = image_stamp
00116         self.put_image_into_scene()


rqt_bag_plugins
Author(s): Aaron Blasdel, Tim Field
autogenerated on Thu Aug 17 2017 02:19:34