Recibo un error del siguiente código de Python3, en las líneas indicadas. x, y, y z son todas matrices numpy simples 2D idénticas pero de tamaño, y deberían funcionar igual. Sin embargo, actúan de manera diferente, con y y z chocando mientras x funciona bien.¿Qué causa AttributeError dependiente de la dimensión en la función PIL fromarray?
import numpy as np
from PIL import Image
a = np.ones((3,3,3), dtype='uint8')
x = a[1,:,:]
y = a[:,1,:]
z = a[:,:,1]
imx = Image.fromarray(x) # ok
imy = Image.fromarray(y) # error
imz = Image.fromarray(z) # error
pero esto funciona
z1 = 1*z
imz = Image.fromarray(z1) # ok
El error es:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python3\lib\site-packages\PIL\Image.py", line 1918, in fromarray
obj = obj.tobytes()
AttributeError: 'numpy.ndarray' object has no attribute 'tobytes'
Entonces, ¿qué hay de diferente entre x, y, z, z1? Nada que yo pueda decir
>>> z.dtype
dtype('uint8')
>>> z1.dtype
dtype('uint8')
>>> z.shape
(3, 4)
>>> z1.shape
(3, 4)
Estoy usando Python 3.2.3 en una máquina con Windows 7 Enterprise, con todo 64 bit.
Ningún error con Python 2.7 en Ubuntu 12.04. – user545424