Create image with PIL `Image.fromarray` results in AttributeError: 'list' object has no attribute '__array_interface__'
Asked Answered
O

3

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')
Outstanding answered 29/12, 2018 at 12:18 Comment(0)
T
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.

Triceps answered 29/12, 2018 at 12:29 Comment(2)
Did you run the code? If I am not mistaken PIL only accepts unsigned integers #27623334Karnes
Thanks! @DanielMesejo I did'nt now updated the code with dtype.Triceps
K
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.

Karnes answered 29/12, 2018 at 12:21 Comment(6)
@MohamedWittiAdou Glad I could help, if you found my answer helpful please consider marking it as accepted.Karnes
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......Outstanding
Just change the way you create np.array, you could create a random matrix of the dimensions you likeKarnes
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 ?Outstanding
What do mean keep the matrix like that? There is a one to one correspondence between the matrix created and the size of the imageKarnes
@MohamedWittiAdou you can create your array as arry = np.array([[1 for _ in range(w)] for _ in range(h)]) where w is width in pixels and h is hieght in pixels. For Example: arry = np.array([[1 for _ in range(400)] for _ in range(200)]) will create an black image of 400 x 200 pixels.Triceps
G
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
Granule answered 23/10 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.