def screencapture(self, filename): format = sdl2.SDL_PIXELFORMAT_ARGB8888 surface = sdl2.SDL_CreateRGBSurfaceWithFormat(0, 800, 600, 32, format) sdl2.SDL_RenderReadPixels(self.wrapped_object.renderer, None, format, surface.pixels, surface.pitch) sdl2.SDL_SaveBMP(surface, filename) sdl2.SDL_FreeSurface(surface)
def write_snapshot(self, filename): """Take a snapshot of the view and write it as a BMP image. Parameters ---------- filename : str Destination filename. """ pixel_size = 4 pixels = self._renderer.read_pixels() surface = sdl2.SDL_CreateRGBSurfaceFrom( pixels.data, pixels.width, pixels.height, 8 * pixel_size, pixels.width * pixel_size, _RGB_MASKS.red, _RGB_MASKS.green, _RGB_MASKS.blue, 0) sdl2.SDL_SaveBMP(surface, filename) sdl2.SDL_FreeSurface(surface)
sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) #This will return a handle to an open 'Notepad.exe' window. window_handle = get_windows_bytitle("Untitled", False) #Create a window so that the hint below can be set a = sdl2.SDL_CreateWindow("test window", sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, 200, 200, 0) #Set hint as recommended by SDL documentation: https://wiki.libsdl.org/SDL_CreateWindowFrom#Remarks result = sdl2.SDL_SetHint(sdl2.SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, hex(id(a))) print(sdl2.SDL_GetError()) np_window = sdl2.SDL_CreateWindowFrom(window_handle[0]) print(sdl2.SDL_GetError()) np_sur = sdl2.SDL_GetWindowSurface(np_window) print(sdl2.SDL_GetError()) save_sur = sdl2.SDL_CreateRGBSurface(0, np_sur[0].w, np_sur[0].h, 32, 0, 0, 0, 0) print(sdl2.SDL_GetError()) r = sdl2.SDL_BlitSurface(np_sur, None, save_sur, None) print(sdl2.SDL_GetError()) result = sdl2.SDL_SaveBMP(save_sur, 'test.bmp') print(sdl2.SDL_GetError()) sdl2.SDL_FreeSurface(save_sur) print(sdl2.SDL_GetError())
# test only if options.copyPacket: pkt2 = copy.copy(pkt) else: pkt2 = pkt pkt2.decode() if pkt2.decoded: decodedCount += 1 print('decoded frame %d' % decodedCount) if decodedCount >= options.offset: print('saving frame...') buf = pkt2.swsFrame.contents.data[0] # convert buffer to a SDL Surface surface = sdl2.SDL_CreateRGBSurfaceFrom(buf, size[0], size[1], 24, size[0] * 3, rmask, gmask, bmask, amask) # save then free surface sdl2.SDL_SaveBMP(surface, 'frame.%d.bmp' % decodedCount) sdl2.SDL_FreeSurface(surface) if decodedCount >= options.offset + options.frameCount: break
def main(): from os import sys if len(sys.argv) <= 1: show_commandline_help() import font from font import Font import layers from layers import HDTextLayer, TextLayer import time import sdl2 t0 = time.clock() import ctypes from ctypes import byref, cast, POINTER, c_int, c_float, sizeof, c_uint32, c_double, c_voidp, c_void_p from sdl2 import endian exp_font_name = sys.argv[1] font_size = 48 if len(sys.argv) > 2: font_size = int(sys.argv[2]) sdl2_DisplayManager.Init(10, 10, 1) sdl2_DisplayManager.inst().fonts_init("Courier", "HHSam") font_path = match_font(exp_font_name) #"coalition") sdl2_DisplayManager.inst().font_add( font_path=font_path, font_alias="export_font", size=font_size) #, color=None, bgcolor=None) char_size = 0 lChars = [chr(i + ord(' ')) for i in xrange(0, 95)] for char_offset, c in zip(xrange(0, 95), lChars): # surf = f.textHollow(c, line_color, interior_color, line_width, fill_color) (font_width, font_height) = sdl2_DisplayManager.inst().font_get_size( c, 'export_font', font_size) char_size = max(char_size, font_width, font_height) width = height = char_size * 10 font_sizes = '' # hack stuff to re-attach the correctly sized window sdl2_DisplayManager.inst().window = sdl2.ext.Window("Font Preview", size=(width, height)) sdl2_DisplayManager.inst().texture_renderer = sdl2.ext.Renderer( sdl2_DisplayManager.inst().window) sdl2_DisplayManager.inst().fill = sdl2_DisplayManager.inst( ).texture_renderer.fill sdl2_DisplayManager.inst().clear = sdl2_DisplayManager.inst( ).texture_renderer.clear sdl2_DisplayManager.inst().factory = sdl2.ext.SpriteFactory( renderer=sdl2_DisplayManager.inst().texture_renderer) sdl2_DisplayManager.inst().show_window() frame = Frame(width, height) #BGR? interior_color = (255, 255, 255) line_width = 0 #fill_color = (255,0,0) line_color = (1, 1, 1) for char_offset, c in zip(xrange(0, 95), lChars): # surf = f.textHollow(c, line_color, interior_color, line_width, fill_color) char_x = char_size * (char_offset % 10) char_y = char_size * (char_offset / 10) surf = sdl2_DisplayManager.inst().font_render_bordered_text_Faster( frame.pySurface, { 'x': char_x, 'y': char_y }, c, font_alias='export_font', size=font_size, border_width=line_width, border_color=line_color, color=interior_color) (font_width, font_height) = sdl2_DisplayManager.inst().font_get_size( c, 'export_font', font_size) #font_sizes += format(font_width,'x') font_sizes += str(font_width) font_sizes += "," sdl2_DisplayManager.inst().screen_blit( source_tx=frame.pySurface, expand_to_fill=True) #, area=(10,10,400,200)) texture_renderer = sdl2_DisplayManager.inst().texture_renderer bk = sdl2.SDL_GetRenderTarget(texture_renderer.renderer) t = sdl2.render.SDL_CreateTexture(texture_renderer.renderer, sdl2.pixels.SDL_PIXELFORMAT_RGBA8888, sdl2.render.SDL_TEXTUREACCESS_TARGET, width, height) #create a new texture and blit the frame to it, then grab bits from that texture_renderer.clear((0, 0, 0, 0)) sdl2.SDL_SetRenderTarget(texture_renderer.renderer, t) #sdl2_DisplayManager.inst().blit(source_tx=frame.pySurface, dest_tx = t, dest = (0,0,512,512)) texture_renderer.copy(frame.pySurface, (0, 0, width, height), (0, 0, width, height)) pitch = c_int() bytes = c_void_p() rect = sdl2.SDL_Rect(0, 0, width, height) sdl2.SDL_LockTexture(t, rect, ctypes.byref(bytes), ctypes.byref(pitch)) if endian.SDL_BYTEORDER == endian.SDL_LIL_ENDIAN: rmask = 0x000000FF gmask = 0x0000FF00 bmask = 0x00FF0000 amask = 0xFF000000 else: rmask = 0xFF000000 gmask = 0x00FF0000 bmask = 0x0000FF00 amask = 0x000000FF print rmask imgsurface = sdl2.surface.SDL_CreateRGBSurfaceFrom(bytes, width, height, 32, pitch, rmask, gmask, bmask, amask) if not imgsurface: raise sdl2.ext.SDLError() sdl2.SDL_RenderReadPixels(texture_renderer.renderer, rect, 0, bytes, pitch) sdl2.SDL_SaveBMP(imgsurface, 'image.png') #4) Restore renderer's texture target sdl2.SDL_SetRenderTarget(texture_renderer.renderer, bk) #ss = sdl2.ext.SoftwareSprite(surf, True) #ss = sdl2.ext.SoftwareSprite(imgsurface, True) #sdl2.SDL_SaveBMP(ss.contents, "file.bmp") # # print font_sizes sdl2_DisplayManager.inst().flip() sdl2.SDL_Delay(2)