def main(): workpath = './image' input_filepaths = [ workpath + '/*.png', workpath + '/*.jpg', workpath + '/*.bmp', ] output_filepath = workpath + '/atlas.png' container_width = 128 options = { 'margin': (1, 1, 1, 1), 'collapse_margin': False, 'enable_auto_size': True, 'enable_vertical_flip': True, 'force_pow2': False } packer.pack( input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options )
def main(): workpath = tempfile.mkdtemp(dir='.') tools.make_random_png32_files(width=(1, 64), height=(1, 64), num_files=10, dirpath=workpath) input_filepaths = [ workpath + '/*.png', ] output_filepath = workpath + '/atlas.png' container_width = 100 options = { 'margin': (1, 1, 1, 1), 'collapse_margin': False, 'enable_auto_size': True, 'enable_vertical_flip': True, 'force_pow2': False } packer.pack( input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options )
def test_collapse_margin(self): with tempfile.TemporaryDirectory() as workpath: tools.make_random_png32_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'output.png') container_width = 66 options = {'margin': (1, 1, 1, 1), 'collapse_margin': True} packer.pack( input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options, ) self.assertTrue(os.path.exists(output_filepath)) self.assertTrue( os.path.exists(os.path.splitext(output_filepath)[0] + '.json'))
def test_combination(self): keys = ('collapse_margin', 'enable_auto_size', 'enable_vertical_flip', 'force_pow2') patterns = ( (True, True, True, True), (True, False, False, False), (True, True, False, False), (True, False, True, False), (True, False, False, True), (True, True, True, False), (True, True, False, True), (True, False, True, True), (False, True, False, False), (False, True, True, False), (False, True, False, True), (False, True, True, True), (False, False, True, False), (False, False, True, True), (False, False, False, True), (False, False, False, False), ) container_width = 66 for pattern in patterns: options = {k: v for k, v in zip(keys, pattern)} options['margin'] = (1, 1, 1, 1) with tempfile.TemporaryDirectory() as workpath: tools.make_random_png32_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'output.png') packer.pack(input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options) self.assertTrue(os.path.exists(output_filepath)) if options['force_pow2']: with Image.open(fp=output_filepath) as im: self.assertTrue(self.is_power_of_2(im.width)) self.assertTrue(self.is_power_of_2(im.height)) self.assertTrue( os.path.exists( os.path.splitext(output_filepath)[0] + '.json'))
def main(): workpath = tempfile.mkdtemp(dir='.') # 1. Make random images. tools.make_random_png24_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) # 2. Pack multiple images of different sizes or formats into one image. input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'atlas.png') container_width = 100 options = { 'margin': (1, 1, 1, 1), 'collapse_margin': False, 'enable_auto_size': True, 'enable_vertical_flip': True, 'force_pow2': False } packer.pack( input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options ) # 3. Unpack it. unpacker = Unpacker( image_filepath=output_filepath, configuration_filepath=os.path.splitext(output_filepath)[0] + '.json' ) unpacker.unpack(dirpath=tempfile.mkdtemp(dir=workpath))
def test_configuration(self): with tempfile.TemporaryDirectory(dir='.') as workpath: tools.make_random_png24_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'output.png') container_width = 100 options = {'margin': (1, 1, 1, 1), 'force_absolute_path': False} packer.pack(input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options) self.assertTrue(os.path.exists(output_filepath)) config_filepath = os.path.splitext(output_filepath)[0] + '.json' self.assertTrue(os.path.exists(config_filepath)) with open(config_filepath, 'r', encoding='utf-8') as fp: config = json.load(fp) filepath = config.get('filepath') self.assertTrue(filepath is not None and isinstance(filepath, str)) self.assertFalse(os.path.isabs(filepath)) width = config.get('width') self.assertTrue(width is not None and isinstance(width, int)) height = config.get('height') self.assertTrue(height is not None and isinstance(height, int)) regions = config.get('regions') self.assertTrue(regions is not None) for region in regions.values(): filepath = region.get('filepath') self.assertTrue(filepath is not None and isinstance(filepath, str)) self.assertFalse(os.path.isabs(filepath)) x = region.get('x') self.assertTrue(x is not None and isinstance(x, int)) y = region.get('y') self.assertTrue(y is not None and isinstance(y, int)) width = region.get('width') self.assertTrue(width is not None and isinstance(width, int)) height = region.get('height') self.assertTrue(height is not None and isinstance(height, int)) with tempfile.TemporaryDirectory(dir='.') as workpath: tools.make_random_png24_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'output.png') container_width = 100 options = {'margin': (1, 1, 1, 1), 'force_absolute_path': True} packer.pack(input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options) self.assertTrue(os.path.exists(output_filepath)) config_filepath = os.path.splitext(output_filepath)[0] + '.json' self.assertTrue(os.path.exists(config_filepath)) with open(config_filepath, 'r', encoding='utf-8') as fp: config = json.load(fp) filepath = config.get('filepath') self.assertTrue(filepath is not None and isinstance(filepath, str)) self.assertTrue(os.path.isabs(filepath)) width = config.get('width') self.assertTrue(width is not None and isinstance(width, int)) height = config.get('height') self.assertTrue(height is not None and isinstance(height, int)) regions = config.get('regions') self.assertTrue(regions is not None) for region in regions.values(): filepath = region.get('filepath') self.assertTrue(filepath is not None and isinstance(filepath, str)) self.assertTrue(os.path.isabs(filepath)) x = region.get('x') self.assertTrue(x is not None and isinstance(x, int)) y = region.get('y') self.assertTrue(y is not None and isinstance(y, int)) width = region.get('width') self.assertTrue(width is not None and isinstance(width, int)) height = region.get('height') self.assertTrue(height is not None and isinstance(height, int))
def test_disable_auto_size(self): with tempfile.TemporaryDirectory() as workpath: tools.make_random_png32_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'output.png') container_width = 66 options = {'margin': (1, 1, 1, 1), 'enable_auto_size': False} packer.pack( input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options, ) self.assertTrue(os.path.exists(output_filepath)) self.assertTrue( os.path.exists(os.path.splitext(output_filepath)[0] + '.json')) with tempfile.TemporaryDirectory() as workpath: tools.make_random_png32_files(width=(1, 64), height=(1, 64), num_files=4, dirpath=workpath) tools.make_random_bmp_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) tools.make_random_jpeg_files(width=(1, 64), height=(1, 64), num_files=3, dirpath=workpath) input_filepaths = [ os.path.join(workpath, '*.*'), ] output_filepath = os.path.join(workpath, 'output.png') container_width = 1 options = {'margin': (1, 1, 1, 1), 'enable_auto_size': False} with self.assertRaises(blf.LocationNotFoundError): packer.pack( input_filepaths=input_filepaths, output_filepath=output_filepath, container_width=container_width, options=options, ) self.assertFalse(os.path.exists(output_filepath)) self.assertFalse( os.path.exists(os.path.splitext(output_filepath)[0] + '.json'))