user2808264
user2808264

Reputation: 569

Attribute error in PIL object

I'm using Debian Linux with Python 2.7. I get an raise AttributeError message

AttributeError: __setitem__

for the lines

lena[mask] = 0
lena[range(400), range(400)] = 255

What am I doing wrong.

from PIL import Image
import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import numpy as np

from scipy import misc
import scipy.misc
import copy


lena = Image.open("/home/pi/Desktop/testc.jpg")
array = lena.convert('L')
array=np.asarray(array)
arr=copy.deepcopy(array)
arr[10:13, 20:23]
arr[100:120] = 255

lx, ly = lena.size
X, Y = np.ogrid[0:lx, 0:ly]
mask = (X - lx/2)**2 + (Y - ly/2)**2 > lx*ly/4
lena[mask] = 0
lena[range(400), range(400)] = 255

Upvotes: 1

Views: 273

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121486

PIL image objects do not support item assignment; lena is your PIL Image object.

Did you mean to assign to the numpy array instead? If so, use:

arr[mask] = 0
arr[:400, :400] = 255

where I replaced the range(400) objects with slice notation (much more efficient).

Upvotes: 2

Related Questions