我正在尝试编写一个应用程序,该应用程序将遍历给定图像的每个像素,获取每个像素的rgb值,将其添加到字典中(以及发生的数量),然后给出最常用的rgb值。
然而,为了能够遍历图像,我需要能够获取它们的大小;事实证明,这不是一件容易的事情。
根据PIL文档,图像对象应该有一个名为'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
一点意义都没有。我做错了什么?
发布于 2016-08-19 15:43:57
image.load
返回没有size
属性的像素访问对象。
def load_image(path=sys.argv[1]):
image = Image.open(path)
im = image.load()
SIZE = image.size
return im
是你想要的
文档用于PIL
发布于 2016-08-19 15:45:43
问题在于PixelAccess类,而不是图像类。
https://stackoverflow.com/questions/39042950
复制相似问题