Lewis
Lewis

Reputation: 139

Getting a type error converting array using PIL

from PIL import Image
import numpy as np

pixels1 = Image.open('image.jpeg')

pixels = pixels1.load()

for i in range(pixels1.size[0]):
    for j in range(pixels1.size[1]):
        pixels[i,j] = (0, 0, 0)

pixels = np.asarray(pixels)
pixels = Image.fromarray(pixels)
pixels.show()

I get this error

TypeError: Cannot handle this data type: (1, 1), |O

Upvotes: 0

Views: 39

Answers (1)

Moshe perez
Moshe perez

Reputation: 1726

The pixels variable is a PixelAccess object, used to access individual pixels. it is not the pixel data itself. if you want to see the modified image, use the pixels1 variable.

from PIL import Image
import numpy as np

pixels1 = Image.open('image.jpg')

pixels = pixels1.load()

for i in range(pixels1.size[0]):
    for j in range(pixels1.size[1]):
        pixels[i,j] = (0, 0, 0)

pixels = np.asarray(pixels1)
pixels = Image.fromarray(pixels)
pixels.show()

however I'm not sure why you convert the modified image to an array and then to image again, you can just do pixels1.show() after the loop.

Upvotes: 1

Related Questions