def pack_test(): # create a MaxRectsPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00) # pack texture images under the directory "test_case/" and name the output images as "test_case". # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. packer.pack("test_image/", "test_image%d", "")
def packCharacters(src, dest): packer = Packer.create(max_width=2048, max_height=2048, shape_padding=4, trim_mode=1, reduce_border_artifacts=True, bg_color=0xffffff00) packer.pack(src, dest)
def generate_spritesheet(src_dir, sheet_name, max_w, max_h): print("# Generating sprite sheet from sprites found in \"" + src_dir + "\"...") print("# \tPacking sprites (this may take a minute for larger sheets)...") texture_border_padding = 2 # create a MaxRectsBinPacker # See here for argument details: https://github.com/wo1fsea/PyTexturePacker/blob/master/README.rst packer = Packer.create( texture_format=".png", max_width=max_w, max_height=max_h, bg_color=(255, 255, 255, 0), enable_rotated=False, # force_square=False, border_padding=texture_border_padding, shape_padding=2, inner_padding=1, trim_mode=1, reduce_border_artifacts=True) # pack texture images under directory <src_dir> and name the output image[s] as <sheet_name>. packer.pack(src_dir, sheet_name) print("# \tCropping sheet...") # Read image and get info new_img_path = sheet_name + ".png" img = Image.open(new_img_path) img_info = img.info tweaked_bbox = img.getbbox() cropped_bbox = img.convert("RGBa").getbbox() if (cropped_bbox == None): bands = "" for band in img.getbands(): bands += band + "," print( "!# \tFailed to convert image to pre-multiplied alpha, skipping cropping step, image bands: " + bands) else: # we know the 0/0 top left point is fine, but add border padding to the cropped w/h tweaked_bbox = (0, 0, cropped_bbox[2] + texture_border_padding, cropped_bbox[3] + texture_border_padding) # crop img = img.crop(tweaked_bbox) print("# \tSaving sheet...") # Re-save texture img.save(new_img_path, **img_info) texture_wh = (tweaked_bbox[2], tweaked_bbox[3]) return texture_wh
def pack_atlas(self): # create a MaxRectsBinPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff, enable_rotated=False) # bg_color=0xffffff00) # pack texture images under directory "test_case/" and name the output images as "test_case". # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. #def (self, bg_color=0x00000000, texture_format=".png", max_width=4096, max_height=4096, enable_rotated=True, # force_square=False, border_padding=2, shape_padding=2, inner_padding=0, trim_mode=0, # reduce_border_artifacts=False): #packer.pack("es/", "traffic_signs_es_%d") packer.pack("sprites/", "spritesheet_%d")
def pack_test(assets_path, output_path): # create a MaxRectsBinPacker packer = Packer.create(max_width=1024, max_height=1024, bg_color=0x00000000, enable_rotated=False, force_square=True, border_padding=0) # pack texture images under directory "test_case/" and name the output images as "test_case". # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. packer.pack("{}/".format(assets_path), output_path, save_plist_func=functools.partial( save_plist, "{}.json".format(output_path)))
def main(): # create a MaxRectsBinPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00, enable_rotated=False) # pack texture images under directory "test_case/" and name the output images as "test_case". # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. packer.pack("all/", "texture%d") plist2json("texture0.plist", "texture0.json") os.system("pngquant 256 < texture0.png > tmp.png") os.system("mv tmp.png texture0.png") os.system("mv -v texture0.png texture0.json ../data/img/")
def pack(targetDirectory): if targetDirectory == None: targetDirectory = "" # create a MaxRectsPacker packer = Packer.create(max_width=4096, max_height=4096) packer.trim_mode = 1 # pack texture images under the directorys "outlines/" and "colors/" and name the output images "test_case". # all images will be packed using the uvs of the images from the first directory imageLocations = [ targetDirectory + "outlines/", targetDirectory + "colors/" ] return packer.packWithMatchingUVs(imageLocations, "test_image%d", targetDirectory + "output/")
def parse(input, output, sheet): sheet_no_ext = os.path.splitext(sheet)[0] sheet_filename = os.path.basename(sheet) sprites = {} paths = {} content = None with open(input) as f: content = f.read() for filename in re.findall(r"#include \"([A-Za-z0-9_.]+)\"", content): with open(filename) as f: content = content.replace("#include \"" + filename + "\"", f.read()) packer = Packer.create(packer_type=CustomPacker, max_width=2048, max_height=2048, bg_color=0x00ffffff, enable_rotated=False) for name, padding, ox in re.findall( r"\$\{([A-Za-z0-9_]+)(\:[0-9]+)?(o[+-][0-9]+)?\}", content): if sprites.get(name) is not None: continue try: key = "${" + name + padding + ox + "}" sprite = Sprite(key, name, padding[1:], ox[1:]) sprites[key] = sprite paths[sprite.path] = True except FileNotFoundError: print("Sprite not found! " + name) sys.exit(1) locations = {} for image_rect in packer.pack([p for p in paths.keys()], sheet_no_ext)[0]: locations[image_rect.image_path] = (image_rect.x, image_rect.y, image_rect.width, image_rect.height) for sprite in sprites.values(): loc = locations[sprite.path] content = content.replace( sprite.key, sprite.getSpec(sheet_filename, loc[0], loc[1], loc[2], loc[3])) with open(output, "w") as f: f.write(content)
def pack(self): exclude = self.get_exclude_assets() # Create output size folder Path(f"./{settings.SPRITE_TILE_SIZE}").mkdir(parents=True, exist_ok=True) files = [ str(f) for f in Path(settings.SLNSW_COMASTERS_PATH).glob( f"*_crop_{settings.SPRITE_TILE_SIZE}*" ) if f.suffix == ".png" and f.name.split("_")[0] not in exclude ] packer = Packer.create( packer_type=MaxRectsPacker, max_width=settings.SPRITE_MAX_WIDTH, max_height=settings.SPRITE_MAX_HEIGHT, bg_color=0x00000000, trim_mode=False, enable_rotated=True, ) packer.pack(files, f"./{settings.SPRITE_TILE_SIZE}/subdivisions_%d")
'dirFile': f['dir'], 'dirName': mem['name'], 'dirNum': f['num'] }) # print("audio " + f['dir'] + " " + str(lib['name']) + " " + str(f['num'])) print("Images: " + str(len(imageRects))) print("Sounds: " + str(len(soundSprite))) if len(imageRects) > 0: if res.opaque: packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffffff, trim_mode=1, enable_rotated=False) else: packer = Packer.create(max_width=2048, max_height=2048, bg_color=0x00ffffff, trim_mode=1, enable_rotated=False) atlas_list = packer._pack(imageRects) for i, atlas in enumerate(atlas_list): print("Pack image " + str(i))
palette.extend( ((i & 0b11100000) << 0, (i & 0b00011100) << 2, (i & 0b00000011) << 6)) assert len(palette) == 768 def rgba_to_8bit(r, g, b, a): return (r & 0b11100000) | ((g & 0b11100000) >> 3) | ((b & 0b11000000) >> 6) # create folder for packed graphics assets if not os.path.exists(GRAPHICS_OUTPUT_PATH): os.makedirs(GRAPHICS_OUTPUT_PATH) # pack graphics assets packer = Packer.create(max_width=TEXTUREMAP_WIDTH, max_height=TEXTUREMAP_HEIGHT, enable_rotated=False, \ border_padding=0, shape_padding=0, atlas_format="json") # TODO also include image filenames to packed_asset_filenames # TODO do we need to quantize to 255 so we can make the background black? idk background = Image.new(mode="RGBA", size=(TEXTUREMAP_WIDTH, TEXTUREMAP_HEIGHT), color=(0, 0, 0, 255)) # TODO needs to be able to take in multiple folders and outupt a texturemap for each # TODO test with an image where the texturepack is not majority black (0x000000FF) # convert packed textures into texturemap for directory in os.listdir(GRAPHICS_PATH): # HACK this is terrible # get the texturepack id from directory name texturepack_id = directory[:3] packer.pack(GRAPHICS_PATH + directory,
def pack_test(): packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffffff00, trim_mode=1) packer.pack("test_image/", "test_image%d")
#author: dkcao by 2018-06-08 import os, sys from PyTexturePacker import Packer #pip install PyTexturePacker print "###########start#####################" curpath = raw_input("Please intput dir:") if curpath == "": curpath = os.getcwd() print curpath name1 = raw_input("Please intput name:") output = os.path.join(curpath,"tmp/") outfile = os.path.join(output,name1 + "%d") if not os.path.exists(output): os.mkdir(output) # create a MaxRectsBinPacker packer = Packer.create(max_width=2048, max_height=2048, bg_color=0xffff00ff) # pack texture images under directory "test_case/" and name the output images as "test_case". # "%d" in output file name "test_case%d" is a placeholder, which is a multipack index, starting with 0. packer.pack(curpath, outfile) print outfile print "###########end#####################"
filename = os.path.basename(str(toCopy)) #print("Copying %s" % str(toCopy)[8:]) if (filename not in ignored_images): copyfile("build/assets/work_en/" + filename, "build/assets/work_jp/" + filename) manualPath = basePath + "patches/images/" + assets[0] + "/" if os.path.exists(manualPath): print("Adding manual replacements for " + assets[0] + ".") for toCopy in Path(manualPath).rglob("*.*"): filename = os.path.basename(str(toCopy)) print("Copying " + filename + "...") copyfile(toCopy, "build/assets/work_jp/" + filename) if assets[0] == "toppage_bg_020": print("Manual override for toppage") packer = Packer.create(max_width=2048, max_height=1920, bg_color=0x00ffffff, enable_rotated=False) else: packer = Packer.create(max_width=2048, max_height=2048, bg_color=0x00ffffff, enable_rotated=False) if str(assets[0][-1]) == '0': packer.pack("build/assets/work_jp/", str(assets[0][:-1]) + "%d", output_path="build/assets/work_out/") else: packer.pack("build/assets/work_jp/", str(assets[0]), output_path="build/assets/work_out/") for asset in assets: asset_png = asset + ".png" jp_asset_plist = str(jp_assets[asset_png])[:-4] + ".plist" jp_asset_png = jp_assets[asset_png] os.remove(jp_asset_png) os.remove(jp_asset_plist) savedir = os.path.dirname(jp_asset_png)