Solved PyQt QImage cna't copy, crash without error code
-
@jsulm I used debugger, it will process to qimage = qimage.copy() this command,then just crash without any error code.And I try to use qimage.isNull() it return False.
-
@darrenleeleelee1 out of curiosity, does it also crash, if you try to not copy it into itself but a different variable?
-
-
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
qimage = qimage.copy() # qimage = qimage.copy(QtCore.QRect(0, 0, 2048, 2048)) this will not crash
So do you want to tell us what
qImage.width()
&qImage.height()
of the image actually are? -
@darrenleeleelee1 Did you check whether the image is a null image as I already suggested?
-
@jsulm
He claimsAnd I try to use qimage.isNull() it return False.
assuming this check is done in the right place.
-
@JonB I need to read more carefully sometimes :-)
-
-
-
@JonB It won't crash!But when I change to
qimage.copy(0, 0, qimage.width(), qimage.height())
then it crash.Even I give to other variable first it still not work
wt = qimage.width() ht = qimage.height() qimage = qimage.copy(0, 0, wt, ht)
-
@darrenleeleelee1 what about
qimage = qimage.copy(QtCore.QRect()) -
@J-Hilk I try it.But also crash.
-
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
qimage.copy(0, 0, qimage.width(), qimage.height())
then it crash
It looks very strange to me, because when looking for
QImage::copy(int x, int y, int w, int h)
, I found:inline QImage copy(int x, int y, int w, int h) const { return copy(QRect(x, y, w, h)); }
What if when you change your code to:
qimage = QtGui.QImage(arr, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16) qimageCopy = qimage.copy()
Does this also crash?
-
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
@JonB It won't crash!But when I change to
qimage.copy(0, 0, qimage.width(), qimage.height())then it crash.Even I give to other variable first it still not work
wt = qimage.width()
ht = qimage.height()
qimage = qimage.copy(0, 0, wt, ht)This all implies that the source
qimage
you start with is the issue.Start by looking at exactly what your
arr = ds.pixel_array
holds/returns. TheQImage()
constructor you use states "it must remain valid". That looks to me like the danger area. I think when you copy a portion of the image Qt takes a copy of its own. But probably not when you copy the whole thing. For example, are you able to do a Pythoncopy()
on it so you have your own separate copy, which you then use for the image? And btw, if you have any threading involved anywhere, please say so...... -
@JonB Sorry I am not really get what did you mean.And I think I don't have any threading.(I think..)
-
@darrenleeleelee1
I have no idea what is behind yourds = dcmread('./5F329172_20170623_CR_2_1_1') arr = ds.pixel_array
It's some external library stuff. (And btw I have no idea whether
arr.shape[1], arr.shape[0]
is the right thing to use. Don't bother to explain, just make sure it's correct for whatever thisds
stuff is.)Try this:
import copy ... arr = copy.copy(ds.pixel_array) # or even: # arr = copy.deepcopy(ds.pixel_array)
before you proceed to
qimage = QtGui.QImage(arr, ...)
. Any better, crash-wise? -
@JonB
I add deepcopy,and it also crash. I add some arr attribute for you.
-
@darrenleeleelee1 I think you should change your code to:
qimage = QtGui.QImage(arr.data, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16)
-
@KroMignon I change but still crash...What is the different between
arr
andarr.data
? -
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
What is the different between arr and arr.data?
arr
is an instance ofnumpy.ndarray
type, according to your code!
Andarr.data
is the data buffer according to numpy documentation ==> https://numpy.org/doc/stable/reference/generated/numpy.ndarray.htmlI think it would be great if you first explain what you want to do.
Perhaps you should take a look at this python script which convertsnumpy.ndatarray
toQImage
and vice versa:
https://kogs-www.informatik.uni-hamburg.de/~meine/software/vigraqt/qimage2ndarray.py