Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image.fromarray cannot be used with array.array

I am using Python 3.6 and Pillow 4.0.0 I am trying to make a PIL Image from an array of values, see the simplified code below and I am getting the following error: AttributeError: 'array.array' object has no attribute '__array_interface__' when calling the Image.fromarray() function.

Why does this happen? When PIL documentation says: Creates an image memory from an object exporting the array interface (using the buffer protocol). and array.array documentations says: Array objects also implement the buffer interface, and may be used wherever bytes-like objects are supported...

from PIL import Image
from array import array

arr = array('B', [100, 150, 200, 250])
im = Image.fromarray(arr)
im.show()
like image 429
masaj Avatar asked Oct 26 '25 12:10

masaj


1 Answers

The array interface is a NumPy concept: ref. Said differently, Image.fromarray can only operate on numpy arrays and not on Standard Python Library array.array.

like image 118
Serge Ballesta Avatar answered Oct 28 '25 01:10

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!