glo*_*nip 4 python image python-imaging-library
我正在尝试编写一个应用程序,该应用程序将遍历给定图像的每个像素,获取每个像素的 rgb 值,将其添加到字典中(以及出现次数),然后向我提供最常用的 rgb 值的纲要。
但是,为了能够遍历图像,我需要能够获取它们的大小;事实证明,这并非易事。
根据PIL 文档,Image 对象应该有一个名为“size”的属性。当我尝试运行该程序时,出现此错误:
AttributeError: 'PixelAccess' object has no attribute 'size'
Run Code Online (Sandbox Code Playgroud)
这是代码:
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
Run Code Online (Sandbox Code Playgroud)
这完全没有意义。我究竟做错了什么?
小智 5
image.load返回一个没有size属性的像素访问对象
def load_image(path=sys.argv[1]):
image = Image.open(path)
im = image.load()
SIZE = image.size
return im
Run Code Online (Sandbox Code Playgroud)
是你想要的
PIL文档