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

Can support for the memoryview type be added as input to _decode (decode.c)? #6933

Closed
bigcat88 opened this issue Feb 9, 2023 · 6 comments · Fixed by #6974
Closed

Can support for the memoryview type be added as input to _decode (decode.c)? #6933

bigcat88 opened this issue Feb 9, 2023 · 6 comments · Fixed by #6974

Comments

@bigcat88
Copy link
Contributor

bigcat88 commented Feb 9, 2023

Data created with help of PyMemoryView_FromMemory function.

Overhead for big images when converting memoryview to bytes is noticeable.

Code located here:

if (!PyArg_ParseTuple(args, "y#", &buffer, &bufsize)) {

Is this possible to easy add support for such feature or not?

@radarhere
Copy link
Member

If I understand correctly, you're asking for a modification to the C API? This has nothing to do with the Python interface of Pillow?

#4532 (comment)

Pillow makes a commitment to stable public interfaces, which are defined at the Python layer. The C interfaces are explicitly internal, and no effort is made to keep them stable even between minor releases, and no support or warning is given when they change. (IIRC, there are/were people using the internal interfaces, but they've always been explicitly on their own with that)

@bigcat88
Copy link
Contributor Author

I'm asking for Image.frombytes method to accept memoryview type for data in addition to bytes, when decoder_name="raw".

Pillow/src/PIL/Image.py

Lines 806 to 832 in 7e3bed6

def frombytes(self, data, decoder_name="raw", *args):
"""
Loads this image with pixel data from a bytes object.
This method is similar to the :py:func:`~PIL.Image.frombytes` function,
but loads data into this image instead of creating a new image object.
"""
# may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
# default format
if decoder_name == "raw" and args == ():
args = self.mode
# unpack data
d = _getdecoder(self.mode, decoder_name, args)
d.setimage(self.im)
s = d.decode(data)
if s[0] >= 0:
msg = "not enough image data"
raise ValueError(msg)
if s[1] != 0:
msg = "cannot decode image data"
raise ValueError(msg)

It should not be very hard, I can make a PR for this if you want.

@radarhere
Copy link
Member

Feel free to do so.

@radarhere radarhere changed the title Can support for the memoryview type be added as input to _decode (decode.c) ? Can support for the memoryview type be added as input to _decode (decode.c)? Feb 10, 2023
@radarhere
Copy link
Member

#6937 or #6974 have been created as possible resolutions to this.

@mara004
Copy link

mara004 commented Jun 18, 2023

Thanks for this improvement! Seems to apply to frombuffer(), too.
It's shipped since v9.5, right?

@hugovk
Copy link
Member

hugovk commented Jun 18, 2023

Yes, #6974 was released in https://github.com/python-pillow/Pillow/releases/tag/9.5.0.

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