def widgetshot(widget, filename='output.png'): # detach the widget from the parent parent = widget.parent if parent: parent.remove_widget(widget) # put the widget canvas on a Fbo texture = Texture.create(size=widget.size, colorfmt='rgb') fbo = Fbo(size=widget.size, texture=texture) fbo.add(widget.canvas) # clear the fbo background fbo.bind() fbo.clear_buffer() fbo.release() # draw! fbo.draw() # get the fbo data fbo.bind() data = glReadPixels(0, 0, widget.size[0], widget.size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() # save to a file surf = pygame.image.fromstring(data, widget.size, 'RGBA', True) pygame.image.save(surf, filename) # reattach to the parent if parent: parent.add_widget(widget) return True
def window_flip_and_save(): global dump_idx win = kivy.getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True) filename = '%s%05d.%s' % (dump_prefix, dump_idx, dump_format) pygame.image.save(surface, filename) dump_idx += 1
def screenshot(self, *largs, **kwargs): filename = super(WindowPygame, self).screenshot(*largs, **kwargs) if filename is None: return None from kivy.core.gl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = self.size data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) data = str(buffer(data)) surface = pygame.image.fromstring(data, self.size, 'RGB', True) pygame.image.save(surface, filename) return filename
def screenshot(self, *largs, **kwargs): filename = super(WindowPygame, self).screenshot(*largs, **kwargs) if filename is None: return None from kivy.core.gl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = self.size data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) data = str(buffer(data)) surface = pygame.image.fromstring(data, self.size, 'RGB', True) pygame.image.save(surface, filename) Logger.debug('Window: Screenshot saved at <%s>' % filename) return filename
def screenshot(self, *largs, **kwargs): global glReadPixels, GL_RGBA, GL_UNSIGNED_BYTE filename = super(WindowPygame, self).screenshot(*largs, **kwargs) if filename is None: return None if glReadPixels is None: from kivy.core.gl import glReadPixels, GL_RGBA, GL_UNSIGNED_BYTE width, height = self.system_size data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE) data = str(buffer(data)) surface = pygame.image.fromstring(data, (width, height), 'RGBA', True) pygame.image.save(surface, filename) Logger.debug('Window: Screenshot saved at <%s>' % filename) return filename
def window_flip_and_save(): global img_current win = kivy.getWindow() with lock_current: if not connected: return sem_next.acquire() with lock_current: glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) img_current = str(buffer(data)) sem_current.release()
def save_thumbnail(self, thumbnail_filename, *largs): print 'saving thumbnail to', thumbnail_filename canvas_pos = [int(x) for x in self.pbuilder.particle_window.pos] canvas_size = [int(x) for x in self.pbuilder.particle_window.size] # particle_x = int(self.pbuilder.demo_particle.emitter_x) # particle_y = int(self.pbuilder.demo_particle.emitter_y) # screenshot_y = particle_y - min(canvas_size)/2 # if screenshot_y < canvas_pos[1]: # screenshot_y = canvas_pos[1] # elif screenshot_y > canvas_pos[1] + 0.9*canvas_size[1] - canvas_size[0]: # screenshot_y = canvas_pos[1] + 0.9*canvas_size[1] - canvas_size[0] # data = glReadPixels(canvas_pos[0], screenshot_y, min(canvas_size), min(canvas_size), GL_RGBA, GL_UNSIGNED_BYTE) data = glReadPixels(canvas_pos[0], canvas_pos[1], canvas_size[0], int(0.9*canvas_size[1]), GL_RGBA, GL_UNSIGNED_BYTE) data = str(buffer(data)) image = pygame.image.fromstring(data, (canvas_size[0], int(0.9*canvas_size[1])), 'RGBA', True) pygame.image.save(image, thumbnail_filename)
def _screenshot(): import os import pygame from kivy.core.gl import glReadBuffer, glReadPixels, GL_RGB, \ GL_UNSIGNED_BYTE, GL_FRONT win = getWindow() glReadBuffer(GL_FRONT) data = glReadPixels(0, 0, win.width, win.height, GL_RGB, GL_UNSIGNED_BYTE) surface = pygame.image.fromstring(str(buffer(data)), win.size, 'RGB', True) filename = None for i in xrange(9999): path = os.path.join(os.getcwd(), 'screenshot%04d.jpg' % i) if not os.path.exists(path): filename = path break if filename: try: pygame.image.save(surface, filename) Logger.info('KeyBinding: Screenshot saved at %s' % filename) except: Logger.exception('KeyBinding: Unable to take a screenshot') else: warn = 'KeyBinding: Unable to take screenshot, no more slot available' Logger.warning(warn)