Skip to content

Conversion of image to array fails #3863

@sommerfelt

Description

@sommerfelt

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.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 pro
  • Python: Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
  • Pillow: 6.0.0

doesNotConvertToArray

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions