I am trying to program an application that will loop through every pixel of a given image, get the rgb value for each, add it to a dictionary (along with amount of occurences) and then give me rundown of the most used rgb values.
However, to be able to loop through images, I need to be able to fetch their size; this proved to be no easy task.
According to the PIL documentation, the Image object should have an attribute called 'size'. When I try to run the program, I get this error:
AttributeError: 'PixelAccess' object has no attribute 'size'
this is the code:
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
Which makes no sense at all. What am I doing wrong?
image.load
returns a pixel access object that does not have a size
attribute
def load_image(path=sys.argv[1]):
image = Image.open(path)
im = image.load()
SIZE = image.size
return im
is what you want
documentation for PIL
Note that PIL (Python Imaging Library) is deprecated and replaced by Pillow.
The problem is about the PixelAccess class, not the Image class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With