Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__array_interface__ - get size of image after loading #7032

Closed
bigcat88 opened this issue Mar 22, 2023 · 2 comments · Fixed by #7034
Closed

__array_interface__ - get size of image after loading #7032

bigcat88 opened this issue Mar 22, 2023 · 2 comments · Fixed by #7034
Labels

Comments

@bigcat88
Copy link
Contributor

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.

@radarhere
Copy link
Member

Your request is actually similar to #6186, so sure. I've created PR #7034 to resolve this.

@bigcat88
Copy link
Contributor Author

Thank you very much! You are best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants