Skip to content

__array_interface__ - get size of image after loading #7032

@bigcat88

Description

@bigcat88

To be short, I want to ask to one change that do not break any compatibility, and this case is not usual.

Here is what Pillow has now:

Pillow/src/PIL/Image.py

Lines 686 to 712 in e7fa309

@property
def __array_interface__(self):
# numpy array interface support
new = {}
shape, typestr = _conv_type_shape(self)
new["shape"] = shape
new["typestr"] = typestr
new["version"] = 3
try:
if self.mode == "1":
# Binary images need to be extended from bits to bytes
# See: https://github.com/python-pillow/Pillow/issues/350
new["data"] = self.tobytes("raw", "L")
else:
new["data"] = self.tobytes()
except Exception as e:
if not isinstance(e, (MemoryError, RecursionError)):
try:
import numpy
from packaging.version import parse as parse_version
except ImportError:
pass
else:
if parse_version(numpy.__version__) < parse_version("1.23"):
warnings.warn(e)
raise
return new

I want to ask to move these lines:

        shape, typestr = _conv_type_shape(self)
        new["shape"] = shape
        new["typestr"] = typestr
        new["version"] = 3

at the end , right after before return new

Reason why it is important to me.
I received such an issue where images in HEIF format created by Sony ILCE-7M4 camera has encoded size in itself different from the size value in HEIF header. (HEIF format has size in header and also size of decoded image, and for some unknown reason they can be different)

As I wrote in that issue, I added an optional option ALLOW_INCORRECT_HEADERS(default disabled) that will allow to load such images, and just change the Image.size during load method.
Tested it, and all seems work well, except this array property, cause here first fills image size in _conv_type_shape and after that Image get decoded.

This simple change will do nothing worse, so I will be very appreciated if you agree to move that lines.
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions