Skip to content

Commit 240089c

Browse files
committed
Use a dict to encode the list of channels and dtypes.
Signed-off-by: Chris Lalancette <[email protected]>
1 parent a001a1a commit 240089c

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

image_tools_py/src/cam2image_py.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,20 @@ def flip_image_cb(msg):
4747
# sensor_msgs::Image
4848
def mat2encoding(frame):
4949
encoding = ''
50-
if frame.shape[2] == 1:
51-
encoding += 'mono'
52-
elif frame.shape[2] == 3:
53-
encoding += 'bgr'
54-
elif frame.shape[2] == 4:
55-
encoding += 'rgba'
50+
51+
encodings = {1:'mono', 3:'bgr', 4:'rgba'}
52+
for channels, prefix in encodings.items():
53+
if frame.shape[2] == channels:
54+
encoding += prefix
55+
break
5656
else:
5757
raise Exception("Unsupported frame shape %d" % (frame.shape[2]))
5858

59-
if frame.dtype == 'uint8':
60-
encoding += '8'
61-
elif frame.dtype == 'int16':
62-
encoding += '16'
59+
types = {'uint8':'8', 'int16':'16'}
60+
for dtype, num in types.items():
61+
if frame.dtype == dtype:
62+
encoding += num
63+
break
6364
else:
6465
raise Exception("Unsupported frame type " + frame.dtype)
6566

image_tools_py/src/showimage_py.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@
3030

3131

3232
def encoding2mat(encoding):
33-
if encoding.startswith('mono'):
34-
channels = 1
35-
elif encoding.startswith('bgr'):
36-
channels = 3
37-
elif encoding.startswith('rgba'):
38-
channels = 4
33+
encodings = {'mono' : 1, 'bgr' : 3, 'rgba' : 4}
34+
for prefix, channels in encodings.items():
35+
if encoding.startswith(prefix):
36+
break
3937
else:
4038
raise Exception("Unsupported encoding " + encoding)
4139

42-
if encoding[channels:] == '8':
43-
dtype = 'uint8'
44-
elif encoding[channels:] == '16':
45-
dtype = 'int16'
40+
types = {'8' : 'uint8', '16' : 'int16'}
41+
for prefix, dtype in types.items():
42+
if encoding[channels:] == prefix:
43+
break
4644
else:
4745
raise Exception("Unsupported encoding " + encoding)
4846

0 commit comments

Comments
 (0)