5

I wanted to display an image from a NumPy array, but I got this error:

Traceback (most recent call last):
  File "E:/wittos/python/SVM/witti svm/arraytoimage.py", line 14, in <module>
   image = Image.fromarray(arry)
  File "C:\Users\MOHAMED-WITTI-ADOU\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\Image.py", line 2483, in fromarray
    arr = obj.__array_interface__
AttributeError: 'list' object has no attribute '__array_interface__'

I would like that you help me to solve this error.

import numpy as np
from PIL import Image

# Create a NumPy array
arry = np.array([3,3])
arry= [[25,25,25],[0,0,0],[0,0,0]]

# Create a PIL image from the NumPy array
image = Image.fromarray(arry)

# Save the image
image.save('image.jpg')

3 Answers 3

5

Your way of creating the numpy array is wrong. You should rather create it as:

arry = np.array([[25,25,25],[0,0,0],[0,0,0]])

Then it will work. Since, you are overwriting the empty numpy array created with normal array.

import numpy as np
from PIL import Image

# Create a NumPy array
arry = np.array([[25,25,25],[0,0,0],[0,0,0]])

# Create a PIL image from the NumPy array
image = Image.fromarray(arry.astype('uint8'))

# Save the image
image.save('image.jpg')

This will work.

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

2 Comments

Did you run the code? If I am not mistaken PIL only accepts unsigned integers stackoverflow.com/questions/27622834/…
Thanks! @DanielMesejo I did'nt now updated the code with dtype.
2

The problem is that you are not creating a numpy array:

# Create a NumPy array
arry = np.array([3,3])
arry= [[25,25,25],[0,0,0],[0,0,0]]

when you do that arry becomes a list of lists, hence the error:

AttributeError: 'list' object has no attribute 'array_interface'

You should do this instead:

import numpy as np
from PIL import Image

# Create a NumPy array
arry = np.array([[25, 25, 25], [0, 0, 0], [0, 0, 0]], dtype=np.uint8)

# Create a PIL image from the NumPy array
image = Image.fromarray(arry)

# Save the image
image.save('image.jpg')

Note that the above specifies the dtype of arry to be np.uint8.

6 Comments

@MohamedWittiAdou Glad I could help, if you found my answer helpful please consider marking it as accepted.
done , but i would like to adjust the size of the created image, what should I do? because I am gonna use it as training image for edge detection. suggestion......
Just change the way you create np.array, you could create a random matrix of the dimensions you like
i wanna keep the matrix like that. just I want to change the width and height of the created image , because it seems too small. is there a way to adjust ?
What do mean keep the matrix like that? There is a one to one correspondence between the matrix created and the size of the image
|
0

You can add the function that verify is data is None like:

# Create a PIL image from the NumPy array
image = Image.fromarray(arry)

if image is None:
    pass

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.