image_helper.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from __future__ import print_function
34 import array
35 try:
36  from cStringIO import StringIO
37 except ImportError:
38  from io import StringIO
39 import sys
40 
41 from PIL import Image
42 from PIL import ImageOps
43 import cairo
44 
45 
46 def imgmsg_to_pil(img_msg, rgba=True):
47  try:
48  if img_msg._type == 'sensor_msgs/CompressedImage':
49  pil_img = Image.open(StringIO(img_msg.data))
50  if pil_img.mode != 'L':
51  pil_img = pil_bgr2rgb(pil_img)
52  else:
53  alpha = False
54  pil_mode = 'RGB'
55  if img_msg.encoding == 'mono8':
56  mode = 'L'
57  elif img_msg.encoding == 'rgb8':
58  mode = 'RGB'
59  elif img_msg.encoding == 'bgr8':
60  mode = 'BGR'
61  elif img_msg.encoding in ['bayer_rggb8', 'bayer_bggr8', 'bayer_gbrg8', 'bayer_grbg8']:
62  mode = 'L'
63  elif img_msg.encoding == 'mono16' or img_msg.encoding == '16UC1':
64  pil_mode = 'F'
65  if img_msg.is_bigendian:
66  mode = 'F;16B'
67  else:
68  mode = 'F;16'
69  elif img_msg.encoding == 'rgba8':
70  mode = 'BGR'
71  alpha = True
72  elif img_msg.encoding == 'bgra8':
73  mode = 'RGB'
74  alpha = True
75  else:
76  raise Exception("Unsupported image format: %s" % img_msg.encoding)
77  pil_img = Image.frombuffer(
78  pil_mode, (img_msg.width, img_msg.height), img_msg.data, 'raw', mode, 0, 1)
79 
80  # 16 bits conversion to 8 bits
81  if pil_img.mode == 'F':
82  pil_img = pil_img.point(lambda i: i * (1. / 256.)).convert('L')
83  pil_img = ImageOps.autocontrast(pil_img)
84 
85  if rgba and pil_img.mode != 'RGBA':
86  pil_img = pil_img.convert('RGBA')
87 
88  return pil_img
89 
90  except Exception as ex:
91  print('Can\'t convert image: %s' % ex, file=sys.stderr)
92  return None
93 
94 
95 def pil_bgr2rgb(pil_img):
96  rgb2bgr = (0, 0, 1, 0,
97  0, 1, 0, 0,
98  1, 0, 0, 0)
99  return pil_img.convert('RGB', rgb2bgr)
100 
101 
102 def pil_to_cairo(pil_img):
103  w, h = pil_img.size
104  data = array.array('c')
105  data.fromstring(pil_img.tostring())
106 
107  return cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, w, h)
def imgmsg_to_pil(img_msg, rgba=True)
Definition: image_helper.py:46


rqt_bag_plugins
Author(s): Aaron Blasdel, Tim Field
autogenerated on Fri Jun 7 2019 22:05:56