def texture_add_to_zip(texture, zip_object, entry_name): size = texture.size fbo = Fbo(size=texture.size, texture=texture) fbo.draw() fbo.bind() if format == 'BMP': data = glReadPixels(0, 0, size[0], size[1], GL_RGB, GL_UNSIGNED_BYTE) else: data = glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() zip_object.writestr(entry_name, data)
def texture_save(texture, filename, format=None): size = texture.size fbo = Fbo(size=texture.size, texture=texture) fbo.draw() fbo.bind() if format == 'BMP': data = glReadPixels(0, 0, size[0], size[1], GL_RGB, GL_UNSIGNED_BYTE) else: data = glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() if format == 'BMP': im = PILImage.fromstring('RGB', size, data) else: im = PILImage.fromstring('RGBA', size, data) im = im.transpose(PILImage.FLIP_TOP_BOTTOM) im.save(filename)
def _render(self, dt): del dt self._dirty = False widget = self.source fbo = self.fbo # detach the widget from the parent parent = widget.parent if parent: parent.remove_display_source(widget) # clear the fbo background fbo.bind() fbo.clear_buffer() fbo.release() self.effect_widget.add_widget(widget.container) fbo.draw() fbo.bind() data = glReadPixels(0, 0, widget.native_size[0], widget.native_size[1], GL_RGB, GL_UNSIGNED_BYTE) fbo.release() self.effect_widget.remove_widget(widget.container) # reattach to the parent if parent: parent.add_display_source(widget) if not self.config['only_send_changes'] or self.prev_data != data: self.prev_data = data self.send(data)
def tick(self, dt) -> None: """Draw image for DMD and send it.""" del dt widget = self.source fbo = self.fbo # detach the widget from the parent parent = widget.parent if parent: parent.remove_widget(widget) self.effect_widget.add_widget(widget) # clear the fbo background fbo.bind() fbo.clear_buffer() fbo.release() fbo.draw() fbo.bind() data = glReadPixels(0, 0, widget.native_size[0], widget.native_size[1], GL_RGB, GL_UNSIGNED_BYTE) fbo.release() # reattach to the parent if parent: self.effect_widget.remove_widget(widget) parent.add_widget(widget) if not self.config['only_send_changes'] or self.prev_data != data: self.prev_data = data self.send(data)
def get_screenshot_as_bytes(self): width, height = size = self.root_window.size pixels = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) image = PIL_Image.frombytes('RGB', size, pixels, 'raw', 'RGB', 0, -1) fp = BytesIO() image.save(fp, format='PNG') fp.seek(0) return fp.read()
def texture_get_data(texture): size = texture.size fbo = Fbo(size=texture.size, texture=texture) fbo.draw() fbo.bind() data = glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() return data
def texture_to_pil_image(texture): size = texture.size fbo = Fbo(size=texture.size, texture=texture) fbo.draw() fbo.bind() data = glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() im = PILImage.fromstring('RGBA', size, data) return im
def _render(self, instance, element, context): fbo, effect_widget, source, settings, first, _, _, _ = instance instance[4] = False instance[6] = False # detach the widget from the parent parent = source.parent if parent and hasattr(parent, "remove_display_source"): parent.remove_display_source(source) effect_widget.add_widget(source.container) fbo.draw() fbo.bind() data = glReadPixels(0, 0, source.native_size[0], source.native_size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() effect_widget.remove_widget(source.container) # reattach to the parent if parent and hasattr(parent, "add_display_source"): parent.add_display_source(source) if not first: # for some reasons we got garbage in the first buffer. we just skip it for now values = {} width = source.native_size[0] height = source.native_size[1] for x, y, name in settings['light_map']: x_pixel = int(x * width) y_pixel = height - int(y * height) if (data[width * y_pixel * 4 + x_pixel * 4 + 3]) == 0: # pixel is transparent value = -1 else: value = (data[width * y_pixel * 4 + x_pixel * 4], data[width * y_pixel * 4 + x_pixel * 4 + 1], data[width * y_pixel * 4 + x_pixel * 4 + 2]) if name not in self._last_color or self._last_color[ name] != value: self._last_color[name] = value values[name] = value self.machine.bcp_processor.send("trigger", name="display_light_player_apply", context=context, values=values, element=element, _silent=True) # clear the fbo background fbo.bind() fbo.clear_buffer() fbo.release()
def fbo_get_pixel_color(self, touch, format='1'): self.fbo.bind() x = touch.x / self.scale - self.fbo_rect.pos[0] y = touch.y / self.scale - self.fbo_rect.pos[1] data = glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) self.fbo.release() if format == '1': c = [ord(a) / 255.0 for a in data] else: c = [ord(a) for a in data] return c
def screenshot(self, *largs, **kwargs): filename = super(WindowSDL, self).screenshot(*largs, **kwargs) if filename is None: return from kivy.graphics.opengl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = self.size data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) self._win.save_bytes_in_png(filename, data, width, height) Logger.debug('Window: Screenshot saved at <%s>' % filename) return filename
def read_draw_layer(self, raw=True): self.fbo.bind() #pixels = glReadPixels(self.fbo_pos[0], self.fbo_pos[1], self.fbo.size[0], self.fbo.size[1], GL_RGBA, GL_UNSIGNED_BYTE) pixels = glReadPixels(0, 0, self.fbo.size[0], self.fbo.size[1], GL_RGBA, GL_UNSIGNED_BYTE) self.fbo.release() if not raw: #get ints pixels = [ord(pixel) for pixel in pixels] #convert to tuple format pixels = zip(pixels[0::4], pixels[1::4],pixels[2::4], pixels[3::4]) return pixels
def download_thumb(self): if self.thumb is None: fbo = self.fbo fbo.draw() fbo.bind() tmp = glReadPixels(0, 0, fbo.size[0], fbo.size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() # remove alpha tmp = list(tmp) del tmp[3::4] tmp = ''.join(tmp) self.thumb = (fbo.size[0], fbo.size[1], tmp)
def screenshot(self, *largs, **kwargs): filename = super(WindowSDL, self).screenshot(*largs, **kwargs) if filename is None: return from kivy.graphics.opengl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = self.size data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) self._win.save_bytes_in_png(filename, data, width, height) return # TODO 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.graphics.opengl 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 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.graphics.opengl import (glReadPixels, GL_RGBA, GL_UNSIGNED_BYTE) width, height = self.system_size data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE) data = bytes(bytearray(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 widget_save_canvas(widget, filename, format): parent = widget.parent if parent: parent.remove_widget(widget) size = (int(widget.size[0]), int(widget.size[1])) texture = Texture.create(size=widget.size, colorfmt='rgba') fbo = Fbo(size=widget.size, texture=texture) fbo.add(widget.canvas) fbo.draw() fbo.bind() data = glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() im = PILImage.fromstring('RGBA', size, data) im = im.transpose(PILImage.FLIP_TOP_BOTTOM) im.save(filename, format) if parent: parent.add_widget(widget) return True
def _real_screenshot(): import tempfile from kivy.core.window import Window from kivy.utils import platform try: fd = tempfile.NamedTemporaryFile(suffix=".png", delete=False) # XXX hack, Window.screenshot doesn't respect our filename screenshot_fn = fd.name.rsplit(".")[-2] + "0001.png" fd.close() if platform == "ios": from kivy.graphics.opengl import glReadPixels, GL_RGBA, GL_UNSIGNED_BYTE from kivy.core.image import ImageLoader # hacky, as Window don't have correct system size (why, i don't # know.) width, height = Window._win._get_gl_size() Window.dispatch("on_draw") data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE) # save using standard imageloader, but in rgba loader = [x for x in ImageLoader.loaders if x.can_save()][0] loader.save(screenshot_fn, width, height, "rgba", data, True) """ # strides issue data = list(data) del data[3::4] data = "".join(data) Window._win.save_bytes_in_png(screenshot_fn, data, width, height) """ else: Window.screenshot(name=fd.name) with open(screenshot_fn, "rb") as fd: data = fd.read() _result[:] = [width, height, base64.encodestring(data)] except: import traceback traceback.print_exc() finally: try: os.unlink(screenshot_fn) os.unlink(fd.name) except: pass _event.set()
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 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 = bytes(data) image = pygame.image.fromstring(data, (canvas_size[0], int(0.9*canvas_size[1])), 'RGBA', True) pygame.image.save(image, thumbnail_filename)
def get_pixel_color(self, x, y): """Returns a binary string of the RGB bytes that make up the slide pixel at the passed x,y coordinates. 2 bytes per pixel, 6 bytes total. This method *does* compensate for different window sizes. Note: This method does not work yet. """ raise NotImplementedError # remove when we fix it. :) # do the Window import here because we don't want to import it at the # top or else we won't be able to set window properties from kivy.core.window import Window # convert the passed x/y to the actual x/y of the Window since it's # possible for the mpf-mc display size to be different than the Window # size x *= Window.width / Window.children[0].width y *= Window.height / Window.children[0].height return glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE)
def _real_screenshot(): import tempfile from kivy.core.window import Window try: fd = tempfile.NamedTemporaryFile(suffix=".png", delete=False) # XXX hack, Window.screenshot doesn't respect our filename screenshot_fn = fd.name.rsplit(".")[-2] + "0001.png" fd.close() """ width, height = Window.size print(width, height) Window.screenshot(name=fd.name) """ from kivy.graphics.opengl import glReadPixels, GL_RGBA, GL_UNSIGNED_BYTE from kivy.core.image import ImageLoader # hacky, as Window don't have correct system size (why, i don't # know.) width, height = Window._win._get_gl_size() Window.dispatch("on_draw") data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE) # save using standard imageloader, but in rgba loader = [x for x in ImageLoader.loaders if x.can_save(fmt="png", is_bytesio=False)][0] loader.save(screenshot_fn, width, height, "rgba", data, True, "png") with open(screenshot_fn, "rb") as fd: data = fd.read() _result[:] = [width, height, base64.encodestring(data).decode("utf8")] except: import traceback traceback.print_exc() finally: try: os.unlink(screenshot_fn) os.unlink(fd.name) except: pass _event.set()
def _render(self, instance, element): fbo, effect_widget, source, settings = instance # detach the widget from the parent parent = source.parent if parent: parent.remove_widget(source) effect_widget.add_widget(source) fbo.draw() fbo.bind() data = glReadPixels(0, 0, source.native_size[0], source.native_size[1], GL_RGB, GL_UNSIGNED_BYTE) fbo.release() # reattach to the parent if parent: effect_widget.remove_widget(source) parent.add_widget(source) values = {} for x, y, name in settings['light_map']: values[name] = (data[source.native_size[0] * y * 3 + x * 3], data[source.native_size[0] * y * 3 + x * 3 + 1], data[source.native_size[0] * y * 3 + x * 3 + 2]) self.machine.bcp_processor.send("trigger", name="display_light_player_apply", values=values, element=element) # clear the fbo background fbo.bind() fbo.clear_buffer() fbo.release()
def screen_area_shot(self,rect,filename): from kivy.graphics.opengl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = rect[2],rect[3] data = glReadPixels(rect[0], rect[1], width, height, GL_RGB, GL_UNSIGNED_BYTE) self._win.save_bytes_in_png(filename, data, width, height) Logger.info('Window: Screenshot %s saved at <%s>' % (str(rect),filename))
def get_pixel(self, x, y): self._p_fbo.bind() pixel = glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) self._p_fbo.release() return map(ord, pixel)
def _window_flip_and_save(*largs): from kivy.graphics.opengl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = window.size pixels = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) screenstream_ctx["data"] = (width, height, "rgb", pixels)
buf = ''.join(map(chr, buf)) tex.blit_buffer(buf, pos=pos, size=size, colorfmt='rgba', bufferfmt='ubyte') return tex def new_color(texture, color, x, y, fbo): with fbo: tex_region = texture.get_region(x, y, 4, 4) fbo.add(color) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) Rectangle(size=(4, 4), pos=(x, y), texture=tex_region) def get_pixels(fbo, (x, y)): fbo.bind() data = glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) fbo.release() c = [ord(a) for a in data] return c def binded_fbo_get_pixels(fbo, (x, y)): data = glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE) c = [ord(a) for a in data] return c _fill_queue = [] _fill_width = 0 _fill_height = 0