Python 中的 PIL 抱怨 PixelAccess 没有 'size' 属性,我做错了什么?

PIL in Python complains that there are no 'size' attributes to a PixelAccess, what am I doing wrong?

我正在尝试编写一个应用程序,该应用程序将循环遍历给定图像的每个像素,获取每个像素的 rgb 值,将其添加到字典(连同出现次数)然后给我 运行最常用的 rgb 值。

但是,为了能够遍历图像,我需要能够获取它们的大小;事实证明,这并非易事。

根据PIL documentation,Image 对象应该有一个名为'size' 的属性。当我尝试 运行 程序时,出现此错误:

AttributeError: 'PixelAccess' object has no attribute 'size'

这是代码:

from PIL import Image
import sys

'''
TODO: 
- Get an image
- Loop through all the pixels and get the rgb values
- append rgb values to dict as key, and increment value by 1
- return a "graph" of all the colours and their occurances

TODO LATER:
- couple similar colours together
'''

SIZE = 0

def load_image(path=sys.argv[1]):
    image = Image.open(path)
    im = image.load()
    SIZE = im.size
    return im

keyValue = {}

# set the image object to variable
image = load_image()
print SIZE

这完全没有意义。我做错了什么?

image.load returns 没有 size 属性的像素访问对象

def load_image(path=sys.argv[1]):
    image = Image.open(path)
    im = image.load()
    SIZE = image.size
    return im

就是你想要的

documentation 为 PIL

注意 PIL (Python Imaging Library) is deprecated and replaced by Pillow.

问题是关于 PixelAccess class,而不是 Image class。