def ConvertQtImageToNumPy(qt_image: QG.QImage): width = qt_image.width() height = qt_image.height() if qt_image.depth() == 1: # this is probably super wrong, but whatever for now depth = 1 else: # 8, 24, 32 etc... depth = qt_image.depth() // 8 data_bytearray = qt_image.bits() if QP.qtpy.PYSIDE2: data_bytes = bytes(data_bytearray) elif QP.qtpy.PYQT5: data_bytes = data_bytearray.asstring(height * width * depth) numpy_image = numpy.fromstring(data_bytes, dtype='uint8').reshape( (height, width, depth)) return numpy_image
def imread(filename): """ Read an image using QT's QImage.load """ qtimg = QImage() if not qtimg.load(filename): # QImage.load() returns false on failure, so raise an exception raise IOError('Unable to load file %s' % filename) if qtimg.depth() == 1: raise IOError('1-bit images currently not supported') # TODO: Warn about other odd formats we don't currently handle properly, # such as the odd 16-bit packed formats QT supports arrayptr = qtimg.bits() # QT may pad the image, so we need to use bytesPerLine, not width for # the conversion to a numpy array bytes_per_pixel = qtimg.depth() // 8 pixels_per_line = qtimg.bytesPerLine() // bytes_per_pixel img_size = pixels_per_line * qtimg.height() * bytes_per_pixel arrayptr.setsize(img_size) img = np.array(arrayptr) # Reshape and trim down to correct dimensions if bytes_per_pixel > 1: img = img.reshape((qtimg.height(), pixels_per_line, bytes_per_pixel)) img = img[:, :qtimg.width(), :] else: img = img.reshape((qtimg.height(), pixels_per_line)) img = img[:, :qtimg.width()] # Strip qt's false alpha channel if needed # and reorder color axes as required if bytes_per_pixel == 4 and not qtimg.hasAlphaChannel(): img = img[:, :, 2::-1] elif bytes_per_pixel == 4: img[:, :, 0:3] = img[:, :, 2::-1] return img
def imread(filename): """ Read an image using QT's QImage.load """ warn( '`qt` plugin is deprecated and will be removed in 0.20. ' 'For alternatives, refer to ' 'https://scikit-image.org/docs/stable/user_guide/visualization.html', FutureWarning, stacklevel=2) qtimg = QImage() if not qtimg.load(filename): # QImage.load() returns false on failure, so raise an exception raise IOError('Unable to load file %s' % filename) if qtimg.depth() == 1: raise IOError('1-bit images currently not supported') # TODO: Warn about other odd formats we don't currently handle properly, # such as the odd 16-bit packed formats QT supports arrayptr = qtimg.bits() # QT may pad the image, so we need to use bytesPerLine, not width for # the conversion to a numpy array bytes_per_pixel = qtimg.depth() // 8 pixels_per_line = qtimg.bytesPerLine() // bytes_per_pixel img_size = pixels_per_line * qtimg.height() * bytes_per_pixel arrayptr.setsize(img_size) img = np.array(arrayptr) # Reshape and trim down to correct dimensions if bytes_per_pixel > 1: img = img.reshape((qtimg.height(), pixels_per_line, bytes_per_pixel)) img = img[:, :qtimg.width(), :] else: img = img.reshape((qtimg.height(), pixels_per_line)) img = img[:, :qtimg.width()] # Strip qt's false alpha channel if needed # and reorder color axes as required if bytes_per_pixel == 4 and not qtimg.hasAlphaChannel(): img = img[:, :, 2::-1] elif bytes_per_pixel == 4: img[:, :, 0:3] = img[:, :, 2::-1] return img
def array_from_image(image: QImage): size = image.size().width() * image.size().height() return np.frombuffer(image.bits(), dtype=np.uint8, count=size * image.depth() // 8)