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

Handle readonly numpy arrays #19

Closed
karlwessel opened this issue Feb 18, 2020 · 4 comments
Closed

Handle readonly numpy arrays #19

karlwessel opened this issue Feb 18, 2020 · 4 comments

Comments

@karlwessel
Copy link

I'm using this module since three years now and works like a charm, thank you!

However since numpy 1.17 I get an error when trying to convert readonly arrays to qimage, like in this minimal working example:

import numpy as np
import qimage2ndarray as q2n
img = np.zeros((32,32))
q2n.array2qimage(img, normalize=False) # works fine
img.flags['WRITEABLE'] = False
q2n.array2qimage(img, normalize=False)

The last line results in the following Exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/users/kwessel/miniconda36/lib/python3.6/site-packages/qimage2ndarray/__init__.py", line 326, in array2qimage
    array = _normalize255(array, normalize)
  File "/users/kwessel/miniconda36/lib/python3.6/site-packages/qimage2ndarray/__init__.py", line 204, in _normalize255
    _np.clip(array, low, high, array)
  File "<__array_function__ internals>", line 6, in clip
  File "/users/kwessel/miniconda36/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 2037, in clip
    return _wrapfunc(a, 'clip', a_min, a_max, out=out, **kwargs)
  File "/users/kwessel/miniconda36/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 61, in _wrapfunc
    return bound(*args, **kwds)
  File "/users/kwessel/miniconda36/lib/python3.6/site-packages/numpy/core/_methods.py", line 132, in _clip
    um.clip, a, min, max, out=out, casting=casting, **kwargs)
  File "/users/kwessel/miniconda36/lib/python3.6/site-packages/numpy/core/_methods.py", line 85, in _clip_dep_invoke_with_casting
    return ufunc(*args, out=out, **kwargs)
ValueError: output array is read-only

An easy fix on my side is to just pass a copy of the numpy array to array2qimage. However, since I assume that QImage already needs its own copy of the image data I wonder if it would be possible to convert the image without having to change the input array or create another copy.

@hmeine
Copy link
Owner

hmeine commented Feb 18, 2020

Am I right that this is not currently part of the testsuite? I mean – the tests run on your machine, with Numpy 1.7, right? Then, would you be willing to submit a PR introducing a test for this?
At first glance, the request to be able to convert a read-only array into a QImage looks totally reasonable, and I think this should be fixed. (Probably, one has to prevent the read-only flag from being inherited.)

@hmeine
Copy link
Owner

hmeine commented Feb 18, 2020

Wow… from reading the code now, I get the feeling that you uncovered a bug introduced by me trying to optimize too much: If no normalization is applied, the latest qimage2ndarray would clip the input array to the 0...255 range in-place!

I guess I can quickly introduce a new test based on your code, disregard my question.

@hmeine
Copy link
Owner

hmeine commented Feb 18, 2020

Thanks for reporting. I just released a bugfix version 1.8.3.

@hmeine hmeine closed this as completed Feb 18, 2020
@karlwessel
Copy link
Author

Now it works. Thank you for the quick fix!

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

No branches or pull requests

2 participants