def pack_atlas(args, dirPath, curr_size): texture_packer = get_packer(args['packing_algorithm'], curr_size, args['maxrects_heuristic']) childDirs = os.listdir(dirPath) index = 0 imagesList = [] # Open all images in the directory and add to the packer input data structure. for currPath in childDirs: file_path = os.path.join(dirPath, currPath) if (currPath.startswith(".") or os.path.isdir(file_path)): continue try: img = Image.open(file_path) texture_packer.add_texture(img.size[0], img.size[1], currPath) imagesList.append((currPath, img)) index += 1 except (IOError): print "ERROR: PIL failed to open file: ", file_path # Pack the textures into an atlas as efficiently as possible. packResult = texture_packer.pack_textures(True, True) return (texture_packer, packResult, imagesList)
def pack_fonts(font_filename, point_size, text, color, atlas_size): texture_packer = get_packer("maxrects", str(atlas_size), "area") font = ImageFont.truetype(font_filename, point_size) image_dict = {} for character in text: size = font.getsize(character) name = "%s_%s_%s" % (os.path.basename(font_filename), str(point_size), character) image_dict[name] = Image.new("RGBA", size, color) draw = ImageDraw.Draw(image_dict[name]) draw.text((0, 0), character, font=font) texture_packer.add_texture(image_dict[name].size[0], image_dict[name].size[1], name) # Pack the textures into an atlas as efficiently as possible. packResult = texture_packer.pack_textures(True, True) return (texture_packer, packResult, image_dict)