Tried to load and convert an image (attached) to a numpy array. Fails on first try, succeeds on second. Strange!
Guessing cause may be that the image is slightly corrupted, but this is not detected or reported by PIL.

import numpy as np
from PIL import Image
# This image is to some extent corrupted, but is displayed correctly by viewing programs:
im = Image.open("doesNotConvertToArray.jpg")
a1 = np.asarray(im) # First conversion fails
print(repr(a1))
# Only a singleton object array is printed:
# array(<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2703x1018 at 0x1AA4F6E1048>, dtype=object)
a2 = np.asarray(im) # Succeeds, strangely
print(a2.shape) # Prints correct shape: (1018, 2703, 3)
I find that if I add in ImageFile.LOAD_TRUNCATED_IMAGES = True, it works.
>>> import numpy as np
>>> from PIL import Image, ImageFile
>>> ImageFile.LOAD_TRUNCATED_IMAGES = True
>>> im = Image.open("doesNotConvertToArray.jpg")
>>> a1 = np.asarray(im)
>>> print(a1.shape)
(1018, 2703, 3)
>>> a2 = np.asarray(im)
>>> print(a2.shape)
(1018, 2703, 3)
@sommerfelt Did that help? Okay to close this issue?
I reported the issue more because it indicates a bug in PIL and not so much because it causes a problem for me. In my case I will use a1.dtype==object to flag a corrupted image rather than cloaking it by setting ImageFile.LOAD_TRUNCATED_IMAGES = True.
So to conclude: For my purposes the issue is closed if I can rely on this behaviour, but for PIL it remains either a bug (an Exception should be raised unless ImageFile.LOAD_TRUNCATED_IMAGES = True) or if you like, it could be called an undocumented feature.
I've created PR #3965 to resolve this, by fixing the inconsistent behaviour.
I had the same problem.
When I opened my images in the grayscale mode, the problem did not appear. I am not expert, I don't know why it worked. But at least this comment may help other people.
To do so:
im = Image.open("doesNotConvertToArray.jpg").convert('L')
a1 = np.asarray(im)
a2 = np.asarray(im)
print a1
print a2
Most helpful comment
I find that if I add in
ImageFile.LOAD_TRUNCATED_IMAGES = True, it works.