Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import png # pip install pypng
- from math import sqrt, ceil
- # Background color of the outputted strip image (RGBA bytes)
- TRANSPARENT_BG = b"\x00\x00\x00\x00"
- # Take the given fixed-sized PNG files, and compile
- # them into a sigle rectangular image from left-to-right, top-to-bottom.
- def png_images_to_strip(input_file_list, output_name=None):
- sprites_per_row = ceil(sqrt(len(input_file_list)))
- # Use the first image as the standard size and metadata to go by.
- with open(input_file_list[0], "rb") as f:
- sprite_w, sprite_h, rows, init_details = png.Reader(file=f).read()
- bytes_per_pixel = 4 if init_details["alpha"] else 3
- if init_details["greyscale"] or init_details["bitdepth"] != 8:
- raise Exception("PNG images must be 32 BPP RGBA or 24 BPP RGB")
- combined_w = sprite_w * sprites_per_row
- combined_h = sprite_h * sprites_per_row
- print("Geneating %dx%d image for %dx%d sprites..." % (combined_w, combined_h, sprite_w, sprite_h))
- # Generate entire bitmap in memory, write to it later
- combined_row = [list(TRANSPARENT_BG * combined_w) for i in range(combined_h)]
- for sprite_index, file_name in enumerate(input_file_list):
- sprite_y = sprite_index // sprites_per_row
- sprite_x = sprite_index % sprites_per_row
- with open(file_name, "rb") as f:
- r = png.Reader(file=f)
- w, h, rows, details = r.read()
- if w != sprite_w or h != sprite_h:
- raise Exception("%r does not match dimensions %dx%d." % (file_name, sprite_w, sprite_h))
- if details["alpha"] != init_details["alpha"]:
- raise Exception("Sequential PNG alpha channel does not match.")
- if details["greyscale"] != init_details["greyscale"]:
- raise Exception("Sequential PNG greyscale setting does not match.")
- if details["bitdepth"] != init_details["bitdepth"]:
- raise Exception("Sequential PNG bit depth does not match.")
- for sprite_row_index, sprite_row in enumerate(rows):
- draw_at_row = (sprite_w * sprite_y) + sprite_row_index
- start = sprite_x * sprite_w
- end = (sprite_x + 1) * sprite_w
- combined_row[draw_at_row][start * bytes_per_pixel:end * bytes_per_pixel] = sprite_row
- w = png.Writer(
- combined_w,
- combined_h,
- greyscale=init_details["greyscale"],
- alpha=init_details["alpha"],
- planes=init_details["planes"],
- bitdepth=init_details["bitdepth"]
- )
- file_name = output_name if output_name else "PNG COMBINED %dx%d.png" % (combined_w, combined_h)
- with open(file_name, "wb") as f:
- w.write(f, combined_row)
- print("Saved as %r." % file_name)
- if __name__ == "__main__":
- # To use, simply pass a list of file paths into "png_images_to_strip()".
- # Optionally set the output parameter.
- # Note that all PNG 'sprites' must be the same dimensions.
- # In this use case sample, the files are individual fixed size animation frame outputs of a blender render
- # rendered to files 0001.png to 0032.png.
- png_images_to_strip(["%.4d.png" % i for i in range(1, 33)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement