def cmap2pixmap(cmap, steps=50): """ Convert a maplotlib colormap into a QPixmap Parameters ---------- cmap : `~matplotlib.colors.Colormap` The colormap to use steps : int The number of color steps in the output. Default=50 Returns ------- pixmap : ``QPixmap`` The QPixmap instance """ sm = cm.ScalarMappable(cmap=cmap) sm.norm.vmin = 0.0 sm.norm.vmax = 1.0 inds = np.linspace(0, 1, steps) rgbas = sm.to_rgba(inds) rgbas = [ QtGui.QColor(int(r * 255), int(g * 255), int(b * 255), int(a * 255)).rgba() for r, g, b, a in rgbas ] im = QtGui.QImage(steps, 1, QtGui.QImage.Format_Indexed8) im.setColorTable(rgbas) for i in range(steps): im.setPixel(i, 0, i) im = im.scaled(100, 100) pm = QtGui.QPixmap.fromImage(im) return pm
def _update_image(self): if not self._frozen: frame = self._webcam.capture_frame() self._image = QtGui.QImage( frame.tostring(), frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888).rgbSwapped() self._data = frame_to_data(frame) self.update()
def on_color_change(self): self._qcolor = mpl_to_qt4_color(self.color()) image = QtGui.QImage(70, 22, QtGui.QImage.Format_RGB32) try: image.fill(self._qcolor) except TypeError: # PySide and old versions of PyQt require a RGBA integer image.fill(self._qcolor.rgba()) pixmap = QtGui.QPixmap.fromImage(image) self.setPixmap(pixmap)
def cmap2pixmap(cmap, steps=50): """Convert a Ginga colormap into a QtGui.QPixmap :param cmap: The colormap to use :type cmap: Ginga colormap instance (e.g. ginga.cmap.get_cmap('gray')) :param steps: The number of color steps in the output. Default=50 :type steps: int :rtype: QtGui.QPixmap """ inds = np.linspace(0, 1, steps) n = len(cmap.clst) - 1 tups = [cmap.clst[int(x * n)] for x in inds] rgbas = [ QtGui.QColor(int(r * 255), int(g * 255), int(b * 255), 255).rgba() for r, g, b in tups ] im = QtGui.QImage(steps, 1, QtGui.QImage.Format_Indexed8) im.setColorTable(rgbas) for i in range(steps): im.setPixel(i, 0, i) im = im.scaled(128, 32) pm = QtGui.QPixmap.fromImage(im) return pm