def get_texture(file, game_data, size): rm = graphics.ResourceManager(game_data.game_type) h = game_data.board.shape[0] w = game_data.board.shape[1] if size: tile_width = int(size[0] / w) tile_height = int(size[1] / h) else: ex = rm.tile_from_state_value(0) tile_width = ex.width tile_height = ex.height texture = pyglet.image.Texture.create(width=tile_width * w, height=tile_height * h) board = game_data.board for row in range(h): for col in range(w): x = col * tile_width y = ((h - 1) * tile_height) - row * tile_height tile_state = board[row][col] if tile_state == constants.Item.Bomb.value: bomb_life = get_bomb_life(game_data, row, col) tile = rm.get_bomb_tile(bomb_life) else: tile = rm.tile_from_state_value(tile_state) img = scale_img(tile, tile_width, tile_height) texture.blit_into(img.get_image_data(), x=x, y=y, z=0) texture.save(file=file, encoder=PNGImageEncoder()) # filename='C:\\tmp\\tmp.png') file.seek(0)
def save(self, filename, *args, **kw): buffer = pyglet.image.get_buffer_manager() with open(filename, mode="wb") as fp: buffer.get_color_buffer().save( filename, file=fp, encoder=PNGImageEncoder(), )
def draw_and_check(self): from pyglet.image.codecs.png import PNGImageEncoder self.window.clear() self.window.switch_to() self.window.dispatch_events() yield self.window.dispatch_events() filename = "{0}.png".format(self.filename) buffer = pyglet.image.get_buffer_manager() buffer.get_color_buffer().save(filename, encoder=PNGImageEncoder()) self.assertImageSavedWithContent(filename)
class TEST_PNG_L_SAVE(base_save.TestSave): texture_file = 'l.png' encoder = PNGImageEncoder()
__docformat__ = 'restructuredtext' __version__ = '$Id: $' import unittest import base_save from pyglet.image.codecs import ImageEncoder, ImageEncodeException from pyglet.image.codecs.png import PNGImageEncoder from pyglet.image import codecs class MockPILImageEncoder(ImageEncoder): """Just raise an encode exception""" def encode(self, image, file, filename): raise ImageEncodeException("Encoder not available") codecs.get_encoders = lambda filename: [ MockPILImageEncoder(), PNGImageEncoder(), ] class TEST_NO_ENCODER_RGB_SAVE(base_save.TestSave): texture_file = 'rgb.png' encoder = None if __name__ == '__main__': unittest.main()
def test_pypng_saving(image_test, image_name): """Test saving images using PyPNG.""" from pyglet.image.codecs.png import PNGImageEncoder image_test.test_image_saving(PNGImageEncoder(), image_name)
def test_pypng_save(self): from pyglet.image.codecs.png import PNGImageEncoder encoder = PNGImageEncoder()