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 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 get_font_array(sz, chars=DEFAULT_CHARS): from qtpy.QtGui import QFont, QPainter, QColor font = QFont() font.setFixedPitch(True) font.setPixelSize(int(sz)) font.setStyleStrategy(QFont.NoAntialias) dummy = QImage(10, 10, QImage.Format_ARGB32) pnt = QPainter(dummy) pnt.setFont(font) metric = pnt.fontMetrics() rct = metric.boundingRect(chars) pnt.end() h = rct.height() w = rct.width() img = QImage(w, h, QImage.Format_ARGB32) paint = QPainter() paint.begin(img) paint.setFont(font) paint.setBrush(QColor(255, 255, 255)) paint.setPen(QColor(255, 255, 255)) paint.drawRect(0, 0, w + 1, h + 1) paint.setPen(QColor(0, 0, 0)) paint.setBrush(QColor(0, 0, 0)) paint.drawText(0, paint.fontMetrics().ascent(), chars) paint.end() try: try: # PyQt4 (at least until Qt 4.8) data = img.bits().asstring(img.numBytes()) except AttributeError: if PYSIDE2: # PySide2 data = bytes(img.bits()) else: # PyQt5 data = img.bits().asstring(img.byteCount()) except SystemError: # PyQt4 (4.11.3) and PyQt5 (5.3.2) on Python 3 return npy = np.frombuffer(data, np.uint8) npy.shape = img.height(), int(img.bytesPerLine() / 4), 4 return npy[:, :, 0]
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 * 3)
def array_from_image(image: QImage): size = image.size().width() * image.size().height() return np.frombuffer(image.bits().asstring(size * image.depth() // 8), dtype=np.uint8)