def draw_image(self, img, rect=None): """ img is either a N*M*3 or N*M*4 numpy array, or a PIL Image rect - a tuple (x, y, w, h) """ from PIL import Image, ImageQt if isinstance(img, np.ndarray): # Numeric array pilimg = Image.fromarray(img) width, height = pilimg.width, pilimg.height draw_img = ImageQt.ImageQt(pilimg) pixmap = QtGui.QPixmap.fromImage(draw_img) elif isinstance(img, Image.Image): width, height = img.width, img.height draw_img = ImageQt.ImageQt(img) pixmap = QtGui.QPixmap.fromImage(draw_img) elif hasattr(img, "bmp_array"): # An offscreen kiva agg context pilimg = Image.fromarray(img.bmp_array) width, height = pilimg.width, pilimg.height draw_img = ImageQt.ImageQt(pilimg) pixmap = QtGui.QPixmap.fromImage(draw_img) elif (isinstance(img, GraphicsContext) and isinstance(img.qt_dc, QtGui.QPixmap) and img.gc.isActive()): # An offscreen Qt kiva context # Calling qpainter.device() appears to introduce a memory leak. # using the display context and calling qpainter.isActive() has # the same outcome. pixmap = img.qt_dc width, height = pixmap.width(), pixmap.height() else: msg = "Cannot render image of type '%r' into Qt4 context." warnings.warn(msg % type(img)) return # create a rect object to draw into if rect is None: dest_rect = QtCore.QRectF(0.0, 0.0, self.width(), self.height()) else: dest_rect = QtCore.QRectF(*rect) # draw using the entire image's data source_rect = QtCore.QRectF(0.0, 0.0, width, height) flip_trans = QtGui.QTransform() flip_trans.scale(1.0, -1.0) pixmap = pixmap.transformed(flip_trans) # draw self.gc.drawPixmap(dest_rect, pixmap, source_rect)
def arc(self, x, y, r, start_angle, end_angle, clockwise=False): sweep_angle = (end_angle - start_angle if not clockwise else start_angle - end_angle) self.path.moveTo(x, y) self.path.arcTo(QtCore.QRectF(x - r, y - r, r * 2, r * 2), np.rad2deg(start_angle), np.rad2deg(sweep_angle))
def clip_to_rect(self, x, y, w, h): """ Clip context to the given rectangular region. Region should be a 4-tuple or a sequence. """ self.gc.setClipRect(QtCore.QRectF(x, y, w, h), operation=QtCore.Qt.IntersectClip)
def clear(self, clear_color=(1.0, 1.0, 1.0, 1.0)): """ """ if len(clear_color) == 4: r, g, b, a = clear_color else: r, g, b = clear_color a = 1.0 self.gc.setBackground(QtGui.QBrush(QtGui.QColor.fromRgbF(r, g, b, a))) self.gc.eraseRect(QtCore.QRectF(0, 0, self.width(), self.height()))
def _window_paint(self, event): if self.control is None: return # self._gc is an image context w = self._gc.width() h = self._gc.height() pilimage = Image.frombuffer("RGBA", (w, h), self._gc, "raw", "RGBA", 0, 1) image = ImageQt.toqimage(pilimage) rect = QtCore.QRectF(0, 0, self.control.width(), self.control.height()) painter = QtGui.QPainter(self.control) painter.drawImage(rect, image)
def _window_paint(self, event): if self.control is None: return # self._gc is an image context w = self._gc.width() h = self._gc.height() data = self._gc.pixel_map.convert_to_argb32string() image = QtGui.QImage(data, w, h, QtGui.QImage.Format_ARGB32) rect = QtCore.QRectF(0, 0, w / self._gc.base_scale, h / self._gc.base_scale) painter = QtGui.QPainter(self.control) painter.drawImage(rect, image)
def draw_rect(self, rect, mode=constants.FILL_STROKE): """ Draw a rect. """ rect = QtCore.QRectF(*rect) if mode == constants.STROKE: save_brush = self.gc.brush() self.gc.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush)) self.gc.drawRect(rect) self.gc.setBrush(save_brush) elif mode in [constants.FILL, constants.EOF_FILL]: self.gc.fillRect(rect, self.gc.brush()) else: self.gc.fillRect(rect, self.gc.brush()) self.gc.drawRect(rect)
def _window_paint(self, event): if self.control is None: return # Convert to Qt's pixel format self._shuffle_copy() # self._gc is an image context w = self._gc.width() h = self._gc.height() image = QtGui.QImage( self._shuffle_buffer, w, h, QtGui.QImage.Format_RGB32 ) rect = QtCore.QRectF( 0, 0, w / self._gc.base_scale, h / self._gc.base_scale ) painter = QtGui.QPainter(self.control) painter.drawImage(rect, image)
def clear_rect(self, rect): """ """ self.gc.eraseRect(QtCore.QRectF(*rect))
def fill_rect(self, rect): """ """ self.gc.fillRect(QtCore.QRectF(*rect), self.gc.brush())
def stroke_rect(self, rect): """ """ self.gc.drawRect(QtCore.QRectF(*rect))
def draw_image(self, img, rect=None): """ img is either a N*M*3 or N*M*4 numpy array, or a Kiva image rect - a tuple (x, y, w, h) """ from kiva import agg def copy_padded(array): """ Pad image width to a multiple of 4 pixels, and minimum dims of 12x12. QImage is very particular about its data. """ y, x, d = array.shape pad = lambda v: (4 - (v % 4)) % 4 nx = max(x + pad(x), 12) ny = max(y, 12) if x == nx and y == ny: return array ret = np.zeros((ny, nx, d), dtype=np.uint8) ret[:y, :x] = array[:] return ret if type(img) == type(np.array([])): # Numeric array if img.shape[2] == 3: format = QtGui.QImage.Format_RGB888 elif img.shape[2] == 4: format = QtGui.QImage.Format_RGB32 width, height = img.shape[:2] copy_array = copy_padded(img) draw_img = QtGui.QImage(img.astype(np.uint8), copy_array.shape[1], height, format) pixmap = QtGui.QPixmap.fromImage(draw_img) elif isinstance(img, agg.GraphicsContextArray): converted_img = img.convert_pixel_format('bgra32', inplace=0) copy_array = copy_padded(converted_img.bmp_array) width, height = img.width(), img.height() draw_img = QtGui.QImage(copy_array.flatten(), copy_array.shape[1], height, QtGui.QImage.Format_RGB32) pixmap = QtGui.QPixmap.fromImage(draw_img) elif (isinstance(img, GraphicsContext) and isinstance(img.qt_dc, QtGui.QPixmap) and img.gc.isActive()): # An offscreen Qt kiva context # Calling qpainter.device() appears to introduce a memory leak. # using the display context and calling qpainter.isActive() has # the same outcome. pixmap = img.qt_dc width, height = pixmap.width(), pixmap.height() else: msg = ("Cannot render image of type '%r' into Qt4 context." % type(img)) warnings.warn(msg) return # create a rect object to draw into if rect is None: dest_rect = QtCore.QRectF(0.0, 0.0, self.width(), self.height()) else: dest_rect = QtCore.QRectF(*rect) # draw using the entire image's data source_rect = QtCore.QRectF(0.0, 0.0, width, height) flip_trans = QtGui.QTransform() flip_trans.scale(1.0, -1.0) pixmap = pixmap.transformed(flip_trans) # draw self.gc.drawPixmap(dest_rect, pixmap, source_rect)