def imread_qt(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_qt(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 bytesPerPixel = qtimg.depth() // 8 pixelsPerLine = qtimg.bytesPerLine() // bytesPerPixel img_size = pixelsPerLine * qtimg.height() * bytesPerPixel arrayptr.setsize(img_size) img = np.array(arrayptr) # Reshape and trim down to correct dimensions if bytesPerPixel > 1: img = img.reshape((qtimg.height(), pixelsPerLine, bytesPerPixel)) img = img[:, :qtimg.width(), :] else: img = img.reshape((qtimg.height(), pixelsPerLine)) img = img[:, :qtimg.width()] # Strip qt's false alpha channel if needed # and reorder color axes as required if bytesPerPixel == 4 and not qtimg.hasAlphaChannel(): img = img[:, :, 2::-1] elif bytesPerPixel == 4: img[:, :, 0:3] = img[:, :, 2::-1] return img
def qt_fromWinHBITMAP(hdc, bitmap, w, h): bmi = BITMAPINFO() ctypes.memset(ctypes.byref(bmi), 0, ctypes.sizeof(bmi)) bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER) bmi.bmiHeader.biWidth = w bmi.bmiHeader.biHeight = -h bmi.bmiHeader.biPlanes = 1 bmi.bmiHeader.biBitCount = 32 bmi.bmiHeader.biCompression = win32con.BI_RGB bmi.bmiHeader.biSizeImage = w * h * 4 image = QImage(w, h, QImage.Format_ARGB32_Premultiplied) if image.isNull(): return image # Get bitmap bits data = ctypes.create_string_buffer(bmi.bmiHeader.biSizeImage) if ctypes.windll.gdi32.GetDIBits( hdc, bitmap, 0, h, data, ctypes.byref(bmi), win32con.DIB_RGB_COLORS ): # Create image and copy data into image. for y in range(h): dest = image.scanLine(y); offset = y * image.bytesPerLine() dest[:] = data.raw[offset:offset + image.bytesPerLine()] else: logging.error("qt_fromWinHBITMAP(), failed to get bitmap bits") return image
Qt.RoundCap, Qt.RoundJoin)) testpainter.drawPolygon( QPolygonF([ QPointF(.25 * ptx, .25 * pty + 250) for (ptx, pty) in pentagonpts ])) testpainter.setBrush(Qt.NoBrush) testpainter.setPen( QPen(QBrush(QColor(255, 255, 255, 255), Qt.SolidPattern), 3.0, Qt.DashLine, Qt.RoundCap, Qt.RoundJoin)) testpainter.drawPolyline( QPolygonF([QPointF(pts, pty) for (pts, pty) in linepts])) testpainter.end() # add the image command testimgwidth = testimage.width() testimgheight = testimage.height() testimgstride = testimage.bytesPerLine() # not a good way to get the pixel data testimgdata = bytearray(testimgheight * testimgstride) k = 0 for pty in xrange(testimgheight): for ptx in xrange(testimgwidth): pixval = testimage.pixel(ptx, pty) (aval, rgbval) = divmod(pixval, 256 * 256 * 256) (rval, gbval) = divmod(rgbval, 256 * 256) (gval, bval) = divmod(gbval, 256) testimgdata[k] = bval k += 1 testimgdata[k] = gval k += 1 testimgdata[k] = rval k += 1