解决 TypeError: 'NoneType' object has no attribute '__getitem__'

其实这个问题的出现一般是某个变量是空,也就是'NoneType'类型导致的,大致按照这个方向排错即可解决。最近在处理一个图片问题的时候,大致写的代码是把某个路径下面的图片找到,读到内存里面,在依据x1,y1; x2, y2四个坐标值把图片的ROI扣下来,其中有一段代码是这样写的:

# 遍历
                for every_capture_image_dict in content_dict["captures"]:
                    capture_pic_name = every_capture_image_dict["big_image"]
                    # 读图片
                    img = cv2.imread('../Export/' + Sub_Files + '/' + Same_Person_file + '/'
                                     + capture_pic_name)
                    crop_image = img[int(every_capture_image_dict["position"]["top"]):
                                     int(every_capture_image_dict["position"]["bottom"]),
                                 int(every_capture_image_dict["position"]["left"]):
                                 int(every_capture_image_dict["position"]["right"])]
                    cv2.imwrite('1/' + Same_Person_file + '/' + capture_pic_name, crop_image)

运行的时候报错:TypeError: 'NoneType' object has no attribute '__getitem__',定位的代码行是:

int(every_capture_image_dict["position"]["right"])]

按照报错提示,经过我的思考,我猜想是某个变量为空导致的,而这个代码最有可能是空变量的就是img这个东西虽然被都进来了,但是是空值导致的。辛亏小哥机智,提前把路径给print下来了,然后我找到报错对应的图片路径下面,果然这个路径下面的图片有问题,上图为证

有个图片是不支持的格式,于是果断修改了下代码,改成如下:

# 遍历
                for every_capture_image_dict in content_dict["captures"]:
                    capture_pic_name = every_capture_image_dict["big_image"]
                    # 读图片
                    img = cv2.imread('../Export/' + Sub_Files + '/' + Same_Person_file + '/'
                                     + capture_pic_name)
                    if img is None:
                        continue
                    else:
                        crop_image = img[int(every_capture_image_dict["position"]["top"]):
                                         int(every_capture_image_dict["position"]["bottom"]),
                                     int(every_capture_image_dict["position"]["left"]):
                                     int(every_capture_image_dict["position"]["right"])]
                        cv2.imwrite('1/' + Same_Person_file + '/' + capture_pic_name, crop_image)

再次运行程序,便顺利通过了!!!

调试程序,本来就是要有耐心的。

已标记关键词 清除标记
<div><p>Hi</p> <p>after including https://launchpad.net/~ytvwld/+archive/ubuntu/syncthing and https://launchpad.net/~nilarimogard/+archive/ubuntu/webupd8/ with add-apt-repository I installed syncthing and syncthing-gtk and tried to start syncthing-gtk but got the following exception and an empty window. </p> <p>Versions: syncthing (0.10.30-3) syncthing-gtk (0.6.4-1~webupd8~trusty0)</p> <p>The OS is a Kubuntu (Ubuntu 14.04.2 LTS)</p> <pre><code> ~$ syncthing-gtk (syncthing-gtk:5924): Gtk-WARNING **: No object called: edit-menu-button-image (syncthing-gtk:5924): Gtk-WARNING **: No object called: edit-menu-button-image Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/syncthing_gtk/app.py", line 114, in do_startup self.setup_statusicon() File "/usr/lib/python2.7/dist-packages/syncthing_gtk/app.py", line 292, in setup_statusicon self.statusicon = get_status_icon(self.iconpath, self["si-menu"]) File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 571, in get_status_icon return StatusIconProxy(*args) File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 484, in __init__ self._load_fallback() File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 517, in _load_fallback self._status_fb = StatusIconBackend(*self._arguments) File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 410, in __init__ self._kde_menu = self._get_popupmenu() File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 372, in _get_popupmenu menu_qt.addAction(self._make_qt_action(menu_child_gtk, menu_qt)) File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 311, in _make_qt_action set_image() File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 307, in set_image action.setIcon(self._get_icon_by_name(icon_name)) File "/usr/lib/python2.7/dist-packages/syncthing_gtk/statusicon.py", line 339, in _get_icon_by_name icon_path = self._gtk_icon_theme.lookup_icon(icon_name, 48, 0).get_filename() AttributeError: 'NoneType' object has no attribute 'get_filename' </code></pre> <p>For a screenshot see http://imgur.com/2vSL9c6</p> <p>Did I miss something?</p><p>该提问来源于开源项目:kozec/syncthing-gtk</p></div>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页