anim-ingest.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import io
  2. import os
  3. from PIL import Image
  4. from colorspacious import cspace_convert
  5. import requests
  6. import numpy as np
  7. import ingest
  8. base = "https://play.pokemonshowdown.com/sprites/ani/"
  9. back_base = "https://play.pokemonshowdown.com/sprites/ani-back/"
  10. def get_all_pokemon() -> list[str]:
  11. return ["abomasnow", "malamar", "porygon", "luxray", "eternatus", "roselia"]
  12. def load_image(base: str, name: str) -> Image:
  13. return Image.open(io.BytesIO(requests.get(base + name + ".gif").content))
  14. def get_all_pixels(im: Image) -> list[tuple[int, int, int]]:
  15. rgb_pixels = []
  16. for fr in range(getattr(im, "n_frames", 1)):
  17. im.seek(fr)
  18. rgb_pixels += [
  19. (r, g, b)
  20. for r, g, b, a in im.convert("RGBA").getdata()
  21. if not ingest.is_outline(r, g, b, a)
  22. ]
  23. return rgb_pixels
  24. with open("database-anim.js", "w") as outfile:
  25. outfile.write("const databaseV2 = [\n")
  26. for name in get_all_pokemon():
  27. print("Ingesting", name, "...")
  28. front = get_all_pixels(load_image(base, name))
  29. back = get_all_pixels(load_image(back_base, name))
  30. rgb_pixels = np.array(front + back)
  31. jab_pixels = cspace_convert(rgb_pixels, "sRGB255", "CAM02-UCS")
  32. stats = [len(rgb_pixels), *ingest.all_stats(jab_pixels), *ingest.all_stats(rgb_pixels)]
  33. outfile.write(f' [ "{name}", {", ".join(str(n) for n in stats)} ],\n')
  34. outfile.write("];\n")