PyQt QImage cna't copy, crash without error code
-
I want to show on a QImage and translate to QPixmap, but when I want to manipulate(copy, scaled, or construct by QPixmap) qimage then the process will crash.But If I copy to a small rectangle like(2048, 2048) It was not crash any more.
This arr is numpy ndarray, shape:(3408, 2235) dtype:uint16, maximum value is 65535 and minimum is 0.
I wonder why the process will crash without error code.
How can I fix it?
from PyQt5 import QtCore, QtGui, QtWidgets from pydicom import dcmread import numpy as np class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(2048, 2048) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.photo = QtWidgets.QLabel(self.centralwidget) self.photo.setGeometry(QtCore.QRect(0, 0, 2048, 2048)) self.photo.setText("") ds = dcmread('./5F329172_20170623_CR_2_1_1') arr = ds.pixel_array print("ndarray shape: ", arr.shape, " dtype: ", arr.dtype) print("ndarray max: ", np.max(arr), " min: ", np.min(arr)) qimage = QtGui.QImage(arr, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16) qimage = qimage.copy() # qimage = qimage.copy(QtCore.QRect(0, 0, 2048, 2048)) this will not crash pixmap = QtGui.QPixmap.fromImage(qimage) self.photo.setPixmap(pixmap) self.photo.setScaledContents(True) self.photo.setObjectName("photo") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
dicom data: 5F329172_20170623_CR_2_1_1
-
@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
-
I want to show on a QImage and translate to QPixmap, but when I want to manipulate(copy, scaled, or construct by QPixmap) qimage then the process will crash.But If I copy to a small rectangle like(2048, 2048) It was not crash any more.
This arr is numpy ndarray, shape:(3408, 2235) dtype:uint16, maximum value is 65535 and minimum is 0.
I wonder why the process will crash without error code.
How can I fix it?
from PyQt5 import QtCore, QtGui, QtWidgets from pydicom import dcmread import numpy as np class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(2048, 2048) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.photo = QtWidgets.QLabel(self.centralwidget) self.photo.setGeometry(QtCore.QRect(0, 0, 2048, 2048)) self.photo.setText("") ds = dcmread('./5F329172_20170623_CR_2_1_1') arr = ds.pixel_array print("ndarray shape: ", arr.shape, " dtype: ", arr.dtype) print("ndarray max: ", np.max(arr), " min: ", np.min(arr)) qimage = QtGui.QImage(arr, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16) qimage = qimage.copy() # qimage = qimage.copy(QtCore.QRect(0, 0, 2048, 2048)) this will not crash pixmap = QtGui.QPixmap.fromImage(qimage) self.photo.setPixmap(pixmap) self.photo.setScaledContents(True) self.photo.setObjectName("photo") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
dicom data: 5F329172_20170623_CR_2_1_1
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
I wonder why the process will crash without error code.
Did you use debugger to see where exactly it is crashing?
You should also check https://doc.qt.io/qt-5/qpixmap.html#isNull and https://doc.qt.io/qt-5/qimage.html#isNull before using the QImage/QPixmap. -
@darrenleeleelee1 said in PyQt QImage cna't copy, crash without error code:
I wonder why the process will crash without error code.
Did you use debugger to see where exactly it is crashing?
You should also check https://doc.qt.io/qt-5/qpixmap.html#isNull and https://doc.qt.io/qt-5/qimage.html#isNull before using the QImage/QPixmap.@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.
-
@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 out of curiosity, does it also crash, if you try to not copy it into itself but a different variable?
-
I want to show on a QImage and translate to QPixmap, but when I want to manipulate(copy, scaled, or construct by QPixmap) qimage then the process will crash.But If I copy to a small rectangle like(2048, 2048) It was not crash any more.
This arr is numpy ndarray, shape:(3408, 2235) dtype:uint16, maximum value is 65535 and minimum is 0.
I wonder why the process will crash without error code.
How can I fix it?
from PyQt5 import QtCore, QtGui, QtWidgets from pydicom import dcmread import numpy as np class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(2048, 2048) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.photo = QtWidgets.QLabel(self.centralwidget) self.photo.setGeometry(QtCore.QRect(0, 0, 2048, 2048)) self.photo.setText("") ds = dcmread('./5F329172_20170623_CR_2_1_1') arr = ds.pixel_array print("ndarray shape: ", arr.shape, " dtype: ", arr.dtype) print("ndarray max: ", np.max(arr), " min: ", np.min(arr)) qimage = QtGui.QImage(arr, arr.shape[1], arr.shape[0], QtGui.QImage.Format_Grayscale16) qimage = qimage.copy() # qimage = qimage.copy(QtCore.QRect(0, 0, 2048, 2048)) this will not crash pixmap = QtGui.QPixmap.fromImage(qimage) self.photo.setPixmap(pixmap) self.photo.setScaledContents(True) self.photo.setObjectName("photo") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
dicom data: 5F329172_20170623_CR_2_1_1
@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?
-
@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.
-
@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?@JonB
and it return 2235 3408
@J-Hilk I copy to tmp and it still crash. -
@JonB
and it return 2235 3408
@J-Hilk I copy to tmp and it still crash. -
@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)
-
@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()) -
@darrenleeleelee1 what about
qimage = qimage.copy(QtCore.QRect())@J-Hilk I try it.But also crash.
-
@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 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?
-
@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 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...... -
@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..)
-
@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? -
@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.
-
@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)