def compress_textures(filenames, dest): utils.pg_init((10, 10)) data = [] for name in filenames: try: textures = PackedTextureGroup(name) except FileNotFoundError: continue image = textures.pack.image rawImageStr = pg.image.tostring(image, 'RGB') compressedImageStr = gzip.compress(rawImageStr) utils.debug('compressed size: {}MB. ratio: {}'.format( len(compressedImageStr) / 1024 ** 2, len(compressedImageStr) / len(rawImageStr) )) textureMetas = [] for p, (id, t) in zip(textures.pack.poses, textures.iter_all()): textureMetas.append(( id, t.xoff, t.yoff, p[0], p[1], t.image.get_width(), t.image.get_height(), )) metaItem = { 'name': textures.name, 'size': image.get_size(), 'format': 'RGB', 'image': compressedImageStr, 'textureMetas': textureMetas, } data.append(metaItem) with open(dest, 'wb') as outfile: pickle.dump(data, outfile, -1)
def packer(map_name='wmap', save_to=None): import texture import packer utils.pg_init() textures = texture.TextureGroup.get_group(map_name) with utils.timeit_context('Packing'): pack = packer.ImagePack(img.image for _, img in textures.iter_all()) if save_to: pg.image.save(pack.image, save_to) else: utils.show_surface(pack.image)
def save_load(): utils.pg_init() import record r = record.Record.load('1') r.save('9') r1 = record.Record.load('9') assert r == r1 r = record.Record.load('anger') r.scenes.get(1) r.scenes.get(3) r.scenes.get(6) r.save('9') r1 = record.Record.load('9') assert r == r1
def pack_all(): import texture import packer utils.pg_init() allNames = ['smap', 'wmap', 'mmap' ] + ['fight{:03d}'.format(i) for i in range(110)] rates = [] for name in allNames: with utils.timeit_context('Load and pack ' + name): try: textures = texture.TextureGroup.get_group(name) pack = packer.ImagePack(img.image for _, img in textures.iter_all()) rates.append(pack.rate) except FileNotFoundError: pack = None pass # if pack: # utils.show_surface(pack.image) pg.quit() import matplotlib.pyplot as plt plt.hist(rates) plt.show()
def pack_all(): import texture import packer utils.pg_init() allNames = [ 'smap', 'wmap', 'mmap' ] + ['fight{:03d}'.format(i) for i in range(110)] rates = [] for name in allNames: with utils.timeit_context('Load and pack ' + name): try: textures = texture.TextureGroup.get_group(name) pack = packer.ImagePack(img.image for _, img in textures.iter_all()) rates.append(pack.rate) except FileNotFoundError: pack = None pass # if pack: # utils.show_surface(pack.image) pg.quit() import matplotlib.pyplot as plt plt.hist(rates) plt.show()
def compress_textures(filenames, dest): utils.pg_init((10, 10)) data = [] for name in filenames: try: textures = PackedTextureGroup(name) except FileNotFoundError: continue image = textures.pack.image rawImageStr = pg.image.tostring(image, 'RGB') compressedImageStr = gzip.compress(rawImageStr) utils.debug('compressed size: {}MB. ratio: {}'.format( len(compressedImageStr) / 1024**2, len(compressedImageStr) / len(rawImageStr))) textureMetas = [] for p, (id, t) in zip(textures.pack.poses, textures.iter_all()): textureMetas.append(( id, t.xoff, t.yoff, p[0], p[1], t.image.get_width(), t.image.get_height(), )) metaItem = { 'name': textures.name, 'size': image.get_size(), 'format': 'RGB', 'image': compressedImageStr, 'textureMetas': textureMetas, } data.append(metaItem) with open(dest, 'wb') as outfile: pickle.dump(data, outfile, -1)
def load_image(name='smap.png'): utils.pg_init() with utils.timeit_context('Load image: ' + name): pg.image.load(name)
def map_size(): import texture utils.pg_init() for name in ('smap', 'mmap', 'wmap'): tg = texture.TextureGroup.get_group(name) print(name, len(tg))
def load_mmap(id=0): import texture utils.pg_init() tg = texture.TextureGroup.get_group('mmap') t1 = tg.get(int(id)).image utils.show_surface(t1)
for (w, h), i in rects: # if currentH - h <= thd and w + x <= W: if w + x <= W: # add to this row pass else: # add to now row x = 0 y += currentH currentH = h results[i].topleft = (x, y) x += w self.rate = sum(w * h for (w, h), _ in rects) / (W * (y + currentH)) screen = utils.pg_init() textures = TextureGroup("smap") rectsOrig = [t.image.get_rect() for _, t in textures.iter_all()] rects = [scale_rect(scale, r) for r in rectsOrig] # rects = rects[:150] # packer = GravityPacker(screen.get_size()) packer = SortPacker(screen.get_size()) packer.pack(rects) colors = [tuple(map(lambda x: int(x * 255), colorsys.hsv_to_rgb(h, 0.45, 0.82))) for h in np.linspace(0, 1, len(rects))] reflesh = True
if cls._data is None: with open(config.dataFile, 'rb') as infile: data = pickle.load(infile) cls._data = {meta['name']: meta for meta in data} if name not in cls._groups: cls._groups[name] = TextureGroup(cls._data[name]) return cls._groups[name] def load_all(self): pass def iter_all(self): yield from self.textures.items() def __len__(self): return len(self.textures) def get(self, id, fail=0): id //= 2 if id not in self.textures: utils.debug('{}: id: {} not in range {}'.format( self.name, id, (0, len(textures) - 1))) return None return self.textures[id] if __name__ == '__main__': utils.pg_init() image = TextureGroup.get_group('smap').image utils.show_surface(image)