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 from io import BytesIO
36 import sys
37 
38 from PIL import Image
39 from PIL import ImageOps
40 try:
41  import cairo
42 except ImportError:
43  import cairocffi as 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(BytesIO(img_msg.data))
50  if pil_img.mode.startswith('BGR'):
51  pil_img = pil_bgr2rgb(pil_img)
52  pil_mode = 'RGB'
53  else:
54  pil_mode = 'RGB'
55  if img_msg.encoding in ['mono8', '8UC1']:
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 in ['bayer_rggb16', 'bayer_bggr16', 'bayer_gbrg16', 'bayer_grbg16']:
64  pil_mode = 'I;16'
65  if img_msg.is_bigendian:
66  mode = 'I;16B'
67  else:
68  mode = 'I;16L'
69  elif img_msg.encoding == 'mono16' or img_msg.encoding == '16UC1':
70  pil_mode = 'F'
71  if img_msg.is_bigendian:
72  mode = 'F;16B'
73  else:
74  mode = 'F;16'
75  elif img_msg.encoding == '32FC1':
76  pil_mode = 'F'
77  if img_msg.is_bigendian:
78  mode = 'F;32BF'
79  else:
80  mode = 'F;32F'
81  elif img_msg.encoding == 'rgba8':
82  mode = 'BGR'
83  elif img_msg.encoding == 'bgra8':
84  mode = 'RGB'
85  else:
86  raise Exception("Unsupported image format: %s" % img_msg.encoding)
87  pil_img = Image.frombuffer(
88  pil_mode, (img_msg.width, img_msg.height), img_msg.data, 'raw', mode, 0, 1)
89 
90  # 16 bits conversion to 8 bits
91  if pil_mode == 'I;16':
92  pil_img = pil_img.convert('I').point(lambda i: i * (1. / 256.)).convert('L')
93 
94  if pil_img.mode == 'F':
95  pil_img = pil_img.point(lambda i: i * (1. / 256.)).convert('L')
96  pil_img = ImageOps.autocontrast(pil_img)
97  pil_img = ImageOps.invert(pil_img)
98 
99  if rgba and pil_img.mode != 'RGBA':
100  pil_img = pil_img.convert('RGBA')
101 
102  return pil_img
103 
104  except Exception as ex:
105  print('Can\'t convert image: %s' % ex, file=sys.stderr)
106  return None
107 
108 
109 def pil_bgr2rgb(pil_img):
110  rgb2bgr = (0, 0, 1, 0,
111  0, 1, 0, 0,
112  1, 0, 0, 0)
113  return pil_img.convert('RGB', rgb2bgr)
114 
115 
116 def pil_to_cairo(pil_img):
117  w, h = pil_img.size
118  data = array.array('c')
119  data.fromstring(pil_img.tostring())
120 
121  return cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, w, h)
rqt_bag_plugins.image_helper.pil_to_cairo
def pil_to_cairo(pil_img)
Definition: image_helper.py:116
rqt_bag_plugins.image_helper.pil_bgr2rgb
def pil_bgr2rgb(pil_img)
Definition: image_helper.py:109
rqt_bag_plugins.image_helper.imgmsg_to_pil
def imgmsg_to_pil(img_msg, rgba=True)
Definition: image_helper.py:46


rqt_bag_plugins
Author(s): Dirk Thomas , Aaron Blasdel , Austin Hendrix , Tim Field
autogenerated on Thu Mar 2 2023 03:43:16