日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ROS-Error】 Can‘t convert image: local variable ‘pil_mode‘ referenced before assignment

發布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ROS-Error】 Can‘t convert image: local variable ‘pil_mode‘ referenced before assignment 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

rqt_bag ***.bag 報錯:

Can't convert image: local variable 'pil_mode' referenced before assignment

?

原因: 源于rqt_bag工具中存在bug,pil_mode 被定義為局部變量,但卻在全局中被使用。

如何查找原因:

1. 首先查找rqt_bag: find / -name rqt_bag

/opt/ros/kinetic/lib/python2.7/dist-packages/rqt_bag

2. cd /opt/ros/kinetic/lib/python2.7/dist-packages/rqt_bag

3. 查找 pil_mode 變量:grep -rn pil_mode

rqt_bag_plugins/image_helper.py:57:??????????? pil_mode = 'RGB'
rqt_bag_plugins/image_helper.py:67:??????????????? pil_mode = 'I;16'
rqt_bag_plugins/image_helper.py:73:??????????????? pil_mode = 'F'
rqt_bag_plugins/image_helper.py:87:??????????????? pil_mode, (img_msg.width, img_msg.height), img_msg.data, 'raw', mode, 0, 1)
rqt_bag_plugins/image_helper.py:90:??????? if pil_mode == 'I;16':

4. 修改源碼

修改前

def imgmsg_to_pil(img_msg, rgba=True):try:if img_msg._type == 'sensor_msgs/CompressedImage':pil_img = Image.open(StringIO(img_msg.data))if pil_img.mode != 'L':pil_img = pil_bgr2rgb(pil_img)else:pil_mode = 'RGB'alpha = Falseif img_msg.encoding == 'mono8':mode = 'L'elif img_msg.encoding == 'rgb8':mode = 'RGB'elif img_msg.encoding == 'bgr8':mode = 'BGR'elif img_msg.encoding in ['bayer_rggb8', 'bayer_bggr8', 'bayer_gbrg8', 'bayer_grbg8']:mode = 'L'elif img_msg.encoding in ['bayer_rggb16', 'bayer_bggr16', 'bayer_gbrg16', 'bayer_grbg16']:pil_mode = 'I;16'if img_msg.is_bigendian:mode='I;16B'else:mode='I;16L'elif img_msg.encoding == 'mono16' or img_msg.encoding == '16UC1':pil_mode = 'F'if img_msg.is_bigendian:mode = 'F;16B'else:mode = 'F;16'elif img_msg.encoding == 'rgba8':mode = 'BGR'alpha = Trueelif img_msg.encoding == 'bgra8':mode = 'RGB'alpha = Trueelse:raise Exception("Unsupported image format: %s" % img_msg.encoding)pil_img = Image.frombuffer(pil_mode, (img_msg.width, img_msg.height), img_msg.data, 'raw', mode, 0, 1)# 16 bits conversion to 8 bitsif pil_mode == 'I;16':pil_img = pil_img.convert('I').point(lambda i: i * (1. / 256.)).convert('L')if pil_img.mode == 'F':pil_img = pil_img.point(lambda i: i * (1. / 256.)).convert('L')pil_img = ImageOps.autocontrast(pil_img)if rgba and pil_img.mode != 'RGBA':pil_img = pil_img.convert('RGBA')return pil_imgexcept Exception as ex:print('Can\'t convert image: %s' % ex, file=sys.stderr)return None

修改后

def imgmsg_to_pil(img_msg, rgba=True):try:pil_mode = 'RGB'if img_msg._type == 'sensor_msgs/CompressedImage':pil_img = Image.open(StringIO(img_msg.data))if pil_img.mode != 'L':pil_img = pil_bgr2rgb(pil_img)else:alpha = Falseif img_msg.encoding == 'mono8':mode = 'L'elif img_msg.encoding == 'rgb8':mode = 'RGB'elif img_msg.encoding == 'bgr8':mode = 'BGR'elif img_msg.encoding in ['bayer_rggb8', 'bayer_bggr8', 'bayer_gbrg8', 'bayer_grbg8']:mode = 'L'elif img_msg.encoding in ['bayer_rggb16', 'bayer_bggr16', 'bayer_gbrg16', 'bayer_grbg16']:pil_mode = 'I;16'if img_msg.is_bigendian:mode='I;16B'else:mode='I;16L'elif img_msg.encoding == 'mono16' or img_msg.encoding == '16UC1':pil_mode = 'F'if img_msg.is_bigendian:mode = 'F;16B'else:mode = 'F;16'elif img_msg.encoding == 'rgba8':mode = 'BGR'alpha = Trueelif img_msg.encoding == 'bgra8':mode = 'RGB'alpha = Trueelse:raise Exception("Unsupported image format: %s" % img_msg.encoding)pil_img = Image.frombuffer(pil_mode, (img_msg.width, img_msg.height), img_msg.data, 'raw', mode, 0, 1)# 16 bits conversion to 8 bitsif pil_mode == 'I;16':pil_img = pil_img.convert('I').point(lambda i: i * (1. / 256.)).convert('L')if pil_img.mode == 'F':pil_img = pil_img.point(lambda i: i * (1. / 256.)).convert('L')pil_img = ImageOps.autocontrast(pil_img)if rgba and pil_img.mode != 'RGBA':pil_img = pil_img.convert('RGBA')return pil_imgexcept Exception as ex:print('Can\'t convert image: %s' % ex, file=sys.stderr)return None

?

總結

以上是生活随笔為你收集整理的【ROS-Error】 Can‘t convert image: local variable ‘pil_mode‘ referenced before assignment的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。