gen9_ingest.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import json
  2. import numpy as np
  3. from colorspacious import cspace_convert
  4. from ingest import is_outline
  5. from anim_ingest import (
  6. get_all_pokemon,
  7. flatten_stats,
  8. compute_stats,
  9. base as old_base,
  10. load_image,
  11. score_clustering_jab,
  12. merge_dist_jab,
  13. score_clustering_rgb,
  14. merge_dist_rgb,
  15. )
  16. new_base = "https://play.pokemonshowdown.com/sprites/gen5/"
  17. png = ".png"
  18. # TODO this could probably be simplified but it works for a first pass
  19. new_pokemon = sorted(
  20. set(get_all_pokemon(new_base, png)) - set(get_all_pokemon(old_base))
  21. )
  22. # hacked together from what's in anim_ingest and ingest
  23. for name in new_pokemon:
  24. print(f"Ingesting {name}...")
  25. image = load_image(new_base, name, png)
  26. # read non-outline pixels of image
  27. rgb_pixels = np.array(
  28. [
  29. (r, g, b)
  30. for r, g, b, a in image.convert("RGBA").getdata()
  31. if not is_outline(r, g, b, a)
  32. ]
  33. )
  34. # convert RGB pixels to CAM02 values
  35. jab_pixels = cspace_convert(rgb_pixels, "sRGB255", "CAM02-UCS")
  36. # compute stats
  37. jab_stats = flatten_stats(
  38. compute_stats(
  39. jab_pixels,
  40. score_clustering_jab,
  41. merge_dist_jab,
  42. )
  43. )[1:]
  44. rgb_stats = flatten_stats(
  45. compute_stats(
  46. rgb_pixels,
  47. score_clustering_rgb,
  48. merge_dist_rgb,
  49. )
  50. )[1:]
  51. # and fuck it, just print it to a new file and we'll copy it in manually
  52. with open("database-gen9.json", "a") as outfile:
  53. outfile.write(json.dumps([name, len(rgb_pixels), *jab_stats, *rgb_stats]) + ",\n")