gloriousCatnip
gloriousCatnip

Reputation: 431

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

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?

Upvotes: 5

Views: 6316

Answers (2)

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22952

Note that PIL (Python Imaging Library) is deprecated and replaced by Pillow.

The problem is about the PixelAccess class, not the Image class.

Upvotes: 1

pussinboots
pussinboots

Reputation: 315

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

Upvotes: 7

Related Questions