Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 264
gloriousCatnip Avatar asked Mar 11 '23 09:03

gloriousCatnip


2 Answers

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

like image 159
pussinboots Avatar answered Mar 16 '23 22:03

pussinboots


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

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

like image 21
Laurent LAPORTE Avatar answered Mar 16 '23 22:03

Laurent LAPORTE