0
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

1
  • Please share the entire error message. Also, why are you accessing the values individually like that? What are you trying to do? Commented Feb 7, 2020 at 2:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.