def capture(self, region=None, selector=None, format=QImage.Format_ARGB32_Premultiplied): """Returns snapshot as QImage. :param region: An optional tuple containing region as pixel coodinates. :param selector: A selector targeted the element to crop on. :param format: The output image format. """ if region is None and selector is not None: region = self.region_for_selector(selector) if region: x1, y1, x2, y2 = region w, h = (x2 - x1), (y2 - y1) image = QImage(QSize(x2, y2), format) painter = QPainter(image) self.main_frame.render(painter) painter.end() image = image.copy(x1, y1, w, h) else: self.main_frame.setScrollBarPolicy(QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff) self.main_frame.setScrollBarPolicy(QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff) self.page.setViewportSize(self.main_frame.contentsSize()) image = QImage(self.page.viewportSize(), format) painter = QPainter(image) self.main_frame.render(painter) painter.end() return image
def copy_to_clipboard(plot): # pass # @staticmethod # def wx_copy_to_clipboard(plot): # # WX specific, though QT implementation is similar using # # QImage and QClipboard # import wx # width, height = plot.outer_bounds # gc = PlotGraphicsContext((width, height), dpi=72) backbuffer = plot.use_backbuffer plot.use_backbuffer = False gc.render_component(plot) plot.use_backbuffer = backbuffer # # # Create a bitmap the same size as the plot # # and copy the plot data to it bmp = gc.bmp_array if gc.format().startswith('bgra'): bmp_rgba = bmp[:,:,[2,1,0,3]] else: bmp_rgba = bmp bitmap=QImage(bmp_rgba.tostring(),width,height, PySide.QtGui.QImage.Format_RGB32) if QApplication.clipboard(): QApplication.clipboard().setImage(bitmap.copy()) else: PySide.QtGui.QMessageBox("Unable to open the clipboard.", "Error")
def copy_to_clipboard(plot): # pass # @staticmethod # def wx_copy_to_clipboard(plot): # # WX specific, though QT implementation is similar using # # QImage and QClipboard # import wx # width, height = plot.outer_bounds # gc = PlotGraphicsContext((width, height), dpi=72) backbuffer = plot.use_backbuffer plot.use_backbuffer = False gc.render_component(plot) plot.use_backbuffer = backbuffer # # # Create a bitmap the same size as the plot # # and copy the plot data to it bmp = gc.bmp_array if gc.format().startswith('bgra'): bmp_rgba = bmp[:, :, [2, 1, 0, 3]] else: bmp_rgba = bmp bitmap = QImage(bmp_rgba.tostring(), width, height, PySide.QtGui.QImage.Format_RGB32) if QApplication.clipboard(): QApplication.clipboard().setImage(bitmap.copy()) else: PySide.QtGui.QMessageBox("Unable to open the clipboard.", "Error")
def toQImage(im, copy=False): if im is None: return QImage() if im.dtype == np.uint8: if len(im.shape) == 2: qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_Indexed8) qim.setColorTable(gray_color_table) return qim.copy() if copy else qim elif len(im.shape) == 3: if im.shape[2] == 3: qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGB888) return qim.copy() if copy else qim elif im.shape[2] == 4: qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_ARGB32) return qim.copy() if copy else qim raise NotImplementedException
def takeScreenshot(self): [x, y, width, height] = self.image_crop frame = self.page().mainFrame() size = frame.contentsSize() size.setWidth(1000) size.setHeight(2000) self.page().setViewportSize(size) image = QImage(self.page().viewportSize(), QImage.Format_ARGB32) painter = QPainter(image) frame.render(painter) painter.end() image1 = image.copy(x, y, width, height) image1.save(self.fileName) self.finished = True
def takeScreenshot(self): [x,y,width,height] = self.image_crop frame = self.page().mainFrame() size = frame.contentsSize() size.setWidth(1000) size.setHeight(2000) self.page().setViewportSize(size) image = QImage(self.page().viewportSize(), QImage.Format_ARGB32) painter = QPainter(image) frame.render(painter) painter.end() image1 = image.copy(x,y,width,height) image1.save(self.fileName) self.finished = True
def project(screen, size, tiles, rotate, tilecolor, backcolor = None): backcolor = Qt.white if backcolor is None else backcolor res = len(tiles) template = QImage(size[0]/res, 2*size[1]/res, QImage.Format_RGB32) screen.setBrush(backcolor) screen.drawEllipse(0, 0, res * template.width()/2, res * template.height()/2) screen.drawEllipse(res * template.width()/2, 0, res * template.width()/2, res * template.height()/2) for y in range(res): r = rotate o = (r + 90) * len(tiles[y])/360 # draw each hemisphere from outside to center sections = [[] for s in range(4)] i = 0 while i < len(tiles[y]) and tiles[y][i].vector[0] < 0: sections[0].append(i) i += 1 while i < len(tiles[y]) and tiles[y][i].vector[0] > tiles[y][i-1].vector[0]: sections[1].append(i) i += 1 while i < len(tiles[y]) and tiles[y][i].vector[0] > 0: sections[2].append(i) i += 1 while i < len(tiles[y]): sections[3].append(i) i += 1 for x in sections[0] + list(reversed(sections[3])) + sections[2] + list(reversed(sections[1])): block = template.copy() xo = x + o if xo > len(tiles[y])-1: xo -= len(tiles[y]) elif xo < 0: xo += len(tiles[y]) v = tiles[y][x].vector sx, sy = [(v[i+1]+1)/2 for i in range(2)] sx = 1 + (sx if v[0] > 0 else -sx) block.fill(tilecolor(tiles[y][xo]).rgb()) screen.drawImage(sx*res*block.width()/2, sy*res*block.height()/2, block)
def copy_to_clipboard(plot): width, height = plot.outer_bounds gc = PlotGraphicsContext((width, height), dpi=72) backbuffer = plot.use_backbuffer plot.use_backbuffer = False gc.render_component(plot) plot.use_backbuffer = backbuffer # Create a bitmap the same size as the plot # and copy the plot data to it bmp = gc.bmp_array cache_bmp = bmp.tobytes() bitmap = QImage(cache_bmp, width+1, height+1, QImage.Format_RGB32) if QApplication.clipboard(): QApplication.clipboard().setImage(bitmap.copy(), QClipboard.Clipboard) else: PySide.QtGui.QMessageBox("Unable to open the clipboard.", "Error")
def numpy_to_qimage(array): """ Returns QImage from an RGB array .""" rows, cols, channels = array.shape array4 = np.zeros((rows, cols, 4), dtype=np.uint8) array4[..., 0:3] = array array4[..., 3] = 255 c0 = array[..., 0].copy() c2 = array[..., 2].copy() array4[..., 0] = c2 array4[..., 2] = c0 string = array4.tostring() img = QImage(string, cols, rows, QImage.Format_ARGB32) # On windows, img `img` holds a reference to `string` and behaves wrongly # when the string goes out of scope. return img.copy()
def project(screen, size, tiles, rotate, tilecolor, backcolor = None): res = max([len(r) for r in tiles]), len(tiles) template = QImage(size[0]/res[0], size[1]/res[1], QImage.Format_RGB32) for y in range(res[1]): for x in range(len(tiles[y])): block = template.copy() r = rotate o = r * len(tiles[y])/360 xo = x + o if xo > len(tiles[y])-1: xo -= len(tiles[y]) elif xo < 0: xo += len(tiles[y]) block.fill(tilecolor(tiles[y][xo]).rgb()) screen.drawImage((x + (res[0] - len(tiles[y]))/2)*block.width(), y*block.height(), block)
def project(screen, size, tiles, rotate, tilecolor, backcolor = None): res = len(tiles) template = QImage(size[0]/res, size[1]/res, QImage.Format_RGB32) for y in range(res): for x in range(res): block = template.copy() r = rotate o = r * res/360 xo = (x + o) * len(tiles[y])/res if xo > len(tiles[y])-1: xo -= len(tiles[y]) elif xo < 0: xo += len(tiles[y]) block.fill(tilecolor(tiles[y][xo]).rgb()) screen.drawImage(x*block.width(), y*block.height(), block)
def from_opencv(self, img_opencv): dst = cv2.cvtColor(img_opencv, cv2.COLOR_BGR2RGB) qim = QImage(dst.data, dst.shape[1], dst.shape[0], dst.strides[0], QImage.Format_RGB888) self.img = qim.copy()