0
votes

I'm a beginner to Python and Image Processing and have been trying to change the pixel values of an image using the following function. However I get an error saying- TypeError: 'instancemethod' object does not support item assignment on the 12th (last but one) line of the code Is there anyway to sort this out? This is my code:

from PIL import Image
import math 
size=width,height=32,32
img=Image.new("L", size, "white")
pix=img.load
for i in range (0,31):
    for j in range (0,31):
        sqsum=i*i+j*j
        sqrt=math.sqrt(sqsum)
        sine=math.sin(sqrt)
        absval=abs(sine)
        pix[i,j]=absval
img.show()
1
Because you didn't call img.load before the loop. Should be pix = img.load(). - Daniel Roseman
Thank you! I've changed it and the code runs fine now. - Venkatesh Seetha

1 Answers

1
votes

In Python, object methods are also objects themselves - an instancemethod object. This means that leaving off the () on an assignment doesn't result in an error, it just doesn't do what you expected it to do. It's a rather common typo.

pix=img.load()
#           ^^

To debug a problem like this, just ask yourself "why is pix an instancemethod?" and work backwards from there. It leads you directly to the assignment statement where the problem originated.