Skip to main content
 首页 » 编程设计

python之取消订阅的对象

2025年05月04日12TianFang

我正在使用 PIL

    im = Image.open(teh_file) 
    if im: 
        colors = im.resize( (1,1), Image.ANTIALIAS).getpixel((0,0)) # simple way to get average color 
        red = colors[0] # and so on, some operations on color data 

问题是,在一些(很少,特别是不知道为什么那些简单的 jpeg)上,我在“colors[0]”行上得到“无法订阅的对象”。尝试过:

if colors:  

为真并继续。

if len(colors): 

给出 'len() of unsized object'

  1. 我应该申请什么条件才不会得到这个异常(exception)?
  2. 问题的原因是什么?

请您参考如下方法:

来自 PIL 文档:

getpixel 
 
im.getpixel(xy) => value or tuple 
 
Returns the pixel at the given position. If the image is a multi-layer image, this method returns a tuple. 

所以看起来你的图片有的是多层的,有的是单层的。