import Image
from numpy import zeros, asarray
YUV = zeros((240, 320, 3), dtype='uint8')
im = Image.fromarray(YUV, mode="YCbCr")
blah = asarray(im)
When I run this (IPython 0.10.1 on Py 2.7.1) it seems to make python read some memory which it shouldn’t be reading. Sometimes it crashes, sometimes it doesn’t but I can surely make it crash by increasing the 320×240 zeros to, say, 3200×2400 and/or calling blah.copy(). Also, if I do:
from matplotlib import pyplot as p
p.subplot(221); p.imshow(blah[:,:,0])
p.subplot(222); p.imshow(blah[:,:,1])
p.subplot(223); p.imshow(blah[:,:,2])
p.subplot(224); p.imshow(blah[:,:,3])
p.gray()
p.show()
I start to see junk memory appearing in blah at around about row 180. What’s going on here? Am I converting from PIL Image to numpy array in a bad way? What is the difference between using array(im) instead of asarray(im), and what is preferred? (note in the former case it does still crash sometimes, but it seems to be more stable and less junk)
(Here’s a related question)
I noticed that your image is YCbCr 3-channel, but you’re displaying 4 channels. Turns out the “junk data” problem is caused by a bug in PIL’s array interface, and a fix was committed in Nov 2010. PIL’s array interface is returning a 4th channel.
I ran your test case under PIL 1.1.7 and see the noise. I commented out the
224subplot and re-ran your test using the latest PIL trunk code and a proper 3-channel array is produced, with no noise. The crashing may also be related, but I couldn’t reproduce that in my environment.