Solved PyQt QImage cna't copy, crash without error code
-
@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 -
@KroMignon I saw this before, but I don't know how to apply to uint16.
-
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
I saw this before, but I don't know how to apply to uint16.
Which version of Qt-Libs are you using?
Format_Grayscale16
is only support starting from Qt 5.13 (cf. QImage.Format)Because of this note from documentation:
Constructs an image with the given width , height and format , that uses an existing memory buffer, data . The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
Perhaps you should specify the number of bytes per line, like this:
qimage = QtGui.QImage(arr.data, arr.shape[1], arr.shape[0], arr.shape[1]*2, QtGui.QImage.Format_Grayscale16)
-
@KroMignon said in PyQt QImage cna't copy, crash without error code:
arr.shape[1]*2
sorry, I read through the QImage API, but I don;t get why I need to add this.
After I add this it seems can work. -
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
sorry, I read through the QImage API, but I don;t get why I need to add this.
Then you don't read carefully ;)
The point is when you only specify width and height: The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.To avoid this, you can use width, height and scanline length in bytes, which is pixel_per_line x pixel_size:
- pixel_per_line is arr.shape[1]
- pixel_size is uint16 which are 2 bytes
==> scanline_size = arr.shape[1]*2
After I add this it seems can work.
Great
-
@KroMignon @J-Hilk @JonB @jsulm thanks all u guys!