-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Change Image.tobytes
return type to ByteArray or add a Image.tobytearray
method
#5815
Comments
Image.tobytes
return type to ByteArray or add a Image.tobytearray
method.Image.tobytes
return type to ByteArray or add a Image.tobytearray
method
Just a note - one day, the deprecation in numpy/numpy#19001 will be removed, probably in NumPy 1.23, paving the way for us to revert #5379 and move from I agree that 2 is undesirable, because we value backwards compatibility. So 3 is most likely the solution - but I don't think it necessarily needs to be a public method, if there's no obvious use case apart from this. |
@radarhere Yes, other use-cases could go through numpy to get the writable buffer: |
(NumPy minor releases (1.x.0) go out twice a year, so 1.23.0 is due out mid-2022.) |
numpy/numpy#20835 has been merged into NumPy, so that will be part of NumPy 1.23. |
Image.tobytes
return type to ByteArray or add a Image.tobytearray
methodImage.tobytes
return type to ByteArray or add a Image.tobytearray
method
@FirefoxMetzger is this resolved? While the internal call to Line 747 in 3b0f046
returns bytes, coming from Line 131 in 3b0f046
|
Currently the method
Image.tobytes
returns a byte string. There is nothing inherently bad about this, except that the returned bytes are read only (ofc) since bytes are immutable.The consequence of this is that
Image.__array__
does an extra copy when callingPillow/src/PIL/Image.py
Line 678 in a3d1e2f
that (I think) can be avoided. The data is copied from the immutable bytes string into a new mutable memory block around which the array is constructed.
This copy can be avoided in a few ways:
Image.__array__
to (roughly)np.frombuffer(self.tobytes(), dtype=dtype).reshape(shape)
. The downside is that the result is read-only (result.flags['WRITEABLE'] == False
) which may be undesirable.Image.tobytes
to return abytearray
. This may require work, depending on whatencoder
returns (didn't dive that far into the code), and it may be undesirable because type(Image.tobytes()) changes.Image.tobytearray
method that essentially mirrorsImage.tobytes
, except that it creates a mutable buffer. This resolves the problems that come with changingImage.tobytes
return type, but may still need more work.Would this be something interesting and/or useful to do?
The text was updated successfully, but these errors were encountered: