WEBVTT
Kind: captions
Language: en

00:00:00.080 --> 00:00:02.899
This is a Pokémon card... made&nbsp;
of Pokémon cards.&nbsp;&nbsp;

00:00:02.899 --> 00:00:07.617
32580 of them, to be precise.
And it’s all done with code.

00:00:07.617 --> 00:00:10.628
But alright, let’s talk about how
these were actually made.

00:00:10.628 --> 00:00:12.760
It all starts with two main&nbsp;
ingredients:

00:00:12.760 --> 00:00:15.429
a reference image
—the target we&nbsp;want to recreate—

00:00:15.429 --> 00:00:20.105
and a database containing (as of&nbsp;
now) every single Pokémon card ever released.

00:00:20.105 --> 00:00:24.828
Now,&nbsp;&nbsp;"every single card" is a bit tricky to define,&nbsp;
since it depends on what we count.&nbsp;

00:00:24.828 --> 00:00:29.843
Are different&nbsp;language versions separate cards?
What about&nbsp;slight reprints?

00:00:29.843 --> 00:00:35.680
But for reference, the database currently
includes 18,454 unique card images.

00:00:35.680 --> 00:00:39.050
I built this database using a script
 that systematically downloaded 

00:00:39.050 --> 00:00:44.345
every available&nbsp;card from an API I found online
(I’ll link&nbsp;all resources in the description).

00:00:44.345 --> 00:00:47.300
Along with the&nbsp;images, the dataset
also includes metadata—

00:00:47.300 --> 00:00:52.680
things&nbsp;like the set each card belongs to, release dates,&nbsp;
and even the full text of each Pokémon’s attacks.&nbsp;

00:00:52.680 --> 00:00:57.640
This metadata also reveals some interesting&nbsp;
insights. For example, these are the most common&nbsp;&nbsp;

00:00:57.640 --> 00:01:02.920
words in Pokémon attack names. A few trends stand&nbsp;
out: there are multiple references to physical&nbsp;&nbsp;

00:01:02.920 --> 00:01:09.040
attacks (like tackle, punch, and kick), but also&nbsp;
elemental moves, energy-based terms, and even&nbsp;&nbsp;

00:01:09.040 --> 00:01:14.920
references to conditions (such as confuse, sleep,&nbsp;
and hypnosis). And here’s the surprising part—some&nbsp;&nbsp;

00:01:14.920 --> 00:01:19.302
of the most common words sound more like they&nbsp;
belong in a battlefield,&nbsp;&nbsp;

00:01:19.302 --> 00:01:23.016
which definitely wasn’t something
I expected to see in Pokémon!

00:01:23.016 --> 00:01:26.002
Now, moving back to the process itself,&nbsp;
the algorithm begins by

00:01:26.002 --> 00:01:30.795
overlaying a grid onto&nbsp;the reference image, 
dividing it into different&nbsp;sections. 

00:01:30.795 --> 00:01:35.132
Each block is going to be swapped out&nbsp;
for one Pokémon card.

00:01:35.132 --> 00:01:39.057
We can choose the size of&nbsp;the grid and
have as many blocks as we want.

00:01:39.057 --> 00:01:42.794
Of course, the more cards we use,
the smaller&nbsp;each one gets. 

00:01:42.794 --> 00:01:47.713
At a certain point, they&nbsp;become so tiny
that details start to blur&nbsp;when zooming in. 

00:01:47.713 --> 00:01:51.320
To address this, we scale up&nbsp;
the entire collage to a higher resolution using&nbsp;&nbsp;

00:01:51.320 --> 00:01:55.400
standard pixel interpolation techniques.&nbsp;
This way, even at a very small size,&nbsp;&nbsp;

00:01:55.400 --> 00:02:00.040
the cards still look sharp and clear up close.
For every block in the grid, the first&nbsp;&nbsp;

00:02:00.040 --> 00:02:04.120
step is to determine its dominant color—the&nbsp;
one that best represents that section of the&nbsp;&nbsp;

00:02:04.120 --> 00:02:09.400
image. The idea is that we’ll later find the&nbsp;
Pokémon card that matches this color best.&nbsp;

00:02:09.400 --> 00:02:13.600
Computers process color as numerical&nbsp;
values—in the RGB model, for example,&nbsp;&nbsp;

00:02:13.600 --> 00:02:17.720
each pixel is a combination of red,&nbsp;
green, and blue intensities. So,&nbsp;&nbsp;

00:02:17.720 --> 00:02:22.640
to summarize all the colors in a block, we need&nbsp;
to condense these numbers into a single value.&nbsp;

00:02:22.640 --> 00:02:26.680
At first, I tried taking the average&nbsp;
color—basically adding up all the red,&nbsp;&nbsp;

00:02:26.680 --> 00:02:31.360
green, and blue values and dividing by the&nbsp;
number of pixels—but this often produced dull,&nbsp;&nbsp;

00:02:31.360 --> 00:02:37.400
grayish tones. So, I switched to using the median&nbsp;
color instead. In math, the median is simply the&nbsp;&nbsp;

00:02:37.400 --> 00:02:42.280
middle value when you line up all the numbers, and&nbsp;
since it’s less affected by outliers, it tends to&nbsp;&nbsp;

00:02:42.280 --> 00:02:47.800
produce a more vibrant and representative color.
Once the dominant color of a block is determined,&nbsp;&nbsp;

00:02:47.800 --> 00:02:52.120
the algorithm searches for the Pokémon card in&nbsp;
the database with the closest matching color.&nbsp;&nbsp;

00:02:52.120 --> 00:02:56.000
We can precompute the median color for every&nbsp;
card in the database since these values don't&nbsp;&nbsp;

00:02:56.000 --> 00:03:01.480
change. But how do we know if a card’s color truly&nbsp;
resembles the dominant color of a block? At what&nbsp;&nbsp;

00:03:01.480 --> 00:03:06.680
point do two colors count as “close enough”?
I started with a simple approach: I treated&nbsp;&nbsp;

00:03:06.680 --> 00:03:12.040
each color as a point in 3D space and measured the&nbsp;
(Euclidean) distance between them. The closer the&nbsp;&nbsp;

00:03:12.040 --> 00:03:17.080
points, the more similar the colors. While this&nbsp;
method works on a basic level, it doesn’t fully&nbsp;&nbsp;

00:03:17.080 --> 00:03:21.840
capture the nuances of how we perceive color.
To improve on this, I explored alternative&nbsp;&nbsp;

00:03:21.840 --> 00:03:27.000
color spaces. You’ve probably heard of&nbsp;
RGB (red, green, blue) and HSB (hue,&nbsp;&nbsp;

00:03:27.000 --> 00:03:31.760
saturation, brightness), but in this case, the&nbsp;
LAB color space proved to be much more effective&nbsp;&nbsp;

00:03:31.760 --> 00:03:36.800
because it’s designed to be perceptually&nbsp;
uniform. In Lab, “L” stands for lightness,&nbsp;&nbsp;

00:03:36.800 --> 00:03:41.400
and “A” and “B” represent color components that&nbsp;
show differences in a way that aligns more closely&nbsp;&nbsp;

00:03:41.400 --> 00:03:46.000
with how the human eye actually perceives them.
Building on this, we can further improve the&nbsp;&nbsp;

00:03:46.000 --> 00:03:50.949
metric by replacing the Euclidean distance with&nbsp;
something more suited to color perception.&nbsp;&nbsp;

00:03:50.949 --> 00:03:56.760
The CIE Delta E 2000 metric is a more sophisticated&nbsp;
measure that quantifies the perceptual difference&nbsp;&nbsp;

00:03:56.760 --> 00:04:01.720
between two colors. Although the math behind&nbsp;
it is complex, it essentially tells us how&nbsp;&nbsp;

00:04:01.720 --> 00:04:06.520
similar two colors look to the human eye.
Since collages can sometimes make the&nbsp;&nbsp;

00:04:06.520 --> 00:04:11.480
original image lose some of its impact, I added&nbsp;
a tweak to give more weight to highly saturated&nbsp;&nbsp;

00:04:11.480 --> 00:04:16.400
colors, keeping things bright and lively.
But of course, the final result also depends&nbsp;&nbsp;

00:04:16.400 --> 00:04:21.480
on the cards available in the dataset, so quick&nbsp;
break for a fun fact: certain types of cards are&nbsp;&nbsp;

00:04:21.480 --> 00:04:27.040
far more common than others. Charizard has more&nbsp;
than twice as many cards as Blastoise or Venusaur,&nbsp;&nbsp;

00:04:27.040 --> 00:04:32.080
and middle-stage evolutions tend to have fewer&nbsp;
versions overall—maybe because they’re not as&nbsp;&nbsp;

00:04:32.080 --> 00:04:39.000
iconic as their base and final forms. Pokémon&nbsp;
cards vastly outnumber Trainer and Energy cards,&nbsp;&nbsp;

00:04:39.000 --> 00:04:43.974
and among them, Water types take the lead,&nbsp;
followed by Grass and Psychic.&nbsp;&nbsp;

00:04:43.974 --> 00:04:50.320
Evolution stages follow a natural pattern, with&nbsp;
Basic being the most common, then Stage 1,&nbsp;and Stage 2. 

00:04:50.320 --> 00:04:55.320
So if we had to pick a single example&nbsp;
of the most common type of card, it would probably&nbsp;&nbsp;

00:04:55.320 --> 00:05:00.560
be Squirtle—a Basic, Water-type, Pokémon.
So Charmander is the most popular of the&nbsp;&nbsp;

00:05:00.560 --> 00:05:03.720
three original starters, Squirtle&nbsp;
is the most representative,&nbsp;&nbsp;

00:05:03.720 --> 00:05:08.521
and Bulbasaur is... well, my favorite.
Alright, back to the algorithm.

00:05:08.521 --> 00:05:14.760
The final color distance metric is a blend of the
CIE&nbsp;Delta E 2000 value and a weighted saturation&nbsp;&nbsp;

00:05:14.760 --> 00:05:19.280
difference, all in the LAB color space.
There will be one card in the database&nbsp;&nbsp;

00:05:19.280 --> 00:05:23.809
that scores the best according to this metric when&nbsp;
compared to the block’s dominant color.

00:05:23.809 --> 00:05:29.240
However,&nbsp;&nbsp;there will also be others that come very close.&nbsp;
To ensure variety and avoid repeating the same&nbsp;&nbsp;

00:05:29.240 --> 00:05:34.200
cards too often, the algorithm doesn’t just&nbsp;
settle for the top match—it instead finds&nbsp;&nbsp;

00:05:34.200 --> 00:05:39.320
multiple candidates for each block. It starts with&nbsp;
a quick search using the Euclidean distance method&nbsp;&nbsp;

00:05:39.320 --> 00:05:44.920
to narrow down the options, then applies the&nbsp;
more computationally expensive CIE Delta E 2000&nbsp;&nbsp;

00:05:44.920 --> 00:05:48.854
metric (with the saturation adjustment)&nbsp;
to the prefiltered candidates. &nbsp;

00:05:48.854 --> 00:05:54.320
Finally,&nbsp;it selects one card at random from the top&nbsp;
candidates, keeping things organic and varied.&nbsp;

00:05:54.320 --> 00:05:59.840
In some cases, very few cards will meet the&nbsp;
criteria, which can lead to repetition once those&nbsp;&nbsp;

00:05:59.840 --> 00:06:06.040
are exhausted. One possible solution would be to&nbsp;
loosen the acceptance threshold, allowing more&nbsp;&nbsp;

00:06:06.040 --> 00:06:11.880
candidates even if they’re not a perfect match.&nbsp;
But this could also mean that the algorithm picks&nbsp;&nbsp;

00:06:11.880 --> 00:06:16.011
suboptimal cards when there are plenty of good&nbsp;
ones to choose from. 

00:06:16.011 --> 00:06:19.102
This is evident when looking at how
 Pokémon cards are distributed 

00:06:19.102 --> 00:06:22.800
along the&nbsp;hue spectrum—
some colors are underrepresented,&nbsp;&nbsp;

00:06:22.800 --> 00:06:25.268
while others are much more common.&nbsp;&nbsp;

00:06:25.268 --> 00:06:27.920
Instead,&nbsp;the algorithm uses an adaptive threshold,

00:06:27.920 --> 00:06:31.800
relaxing the criteria slightly to allow for&nbsp;
a bigger difference, and re-evaluates the&nbsp;&nbsp;

00:06:31.800 --> 00:06:34.762
candidates until enough good matches are found.

00:06:34.762 --> 00:06:37.712
Before placing the selected card into
the final&nbsp;collage,

00:06:37.712 --> 00:06:42.280
we rotate and offset it slightly&nbsp;
at random. This breaks up the grid and gives&nbsp;&nbsp;

00:06:42.280 --> 00:06:45.321
the image a more natural, handcrafted feel.

00:06:45.321 --> 00:06:48.640
 In my&nbsp;opinion, while this creates a
more organic result,&nbsp;&nbsp;

00:06:48.640 --> 00:06:53.840
the perfectly structured grid also has&nbsp;
its charm... but now we have options.&nbsp;

00:06:53.840 --> 00:06:58.734
The algorithm has a variety of settings that&nbsp;
allow us to fine-tune the result. 

00:06:58.734 --> 00:07:04.238
For example,&nbsp;&nbsp;we can adjust the size of the grid, tweak how&nbsp;
strict the color matching needs to be,

00:07:04.238 --> 00:07:09.015
and modify&nbsp;factors like rotation and
offset ranges, to name a&nbsp;few.

00:07:09.015 --> 00:07:13.998
With all these options, we can experiment and
create collages with varying levels of detail.

00:07:13.998 --> 00:07:19.265
Depending on the settings, processing time 
can range from a few minutes to several days. 

00:07:19.265 --> 00:07:23.102
This&nbsp;collage took 2 hours to process
on my personal&nbsp;laptop.

00:07:23.102 --> 00:07:28.819
It’s made up of over 32000 cards, with&nbsp;
around 11000 unique ones.&nbsp;&nbsp;

00:07:28.819 --> 00:07:33.825
The most repeated card was this one,
appearing 331 times.

00:07:33.825 --> 00:07:36.724
By the way, speaking of repeated cards,
it’s interesting to note that

00:07:36.724 --> 00:07:40.744
some Pokémon&nbsp;are much more represented
than others in the&nbsp;card database. &nbsp;

00:07:40.744 --> 00:07:45.360
Iconic Pokémon like Pikachu,&nbsp;
Charizard, Mewtwo, and Eevee dominate the top&nbsp;&nbsp;

00:07:45.360 --> 00:07:49.036
spots, while others have only one card.&nbsp;&nbsp;

00:07:49.036 --> 00:07:53.672
Fun&nbsp;fact, Unown R actually has fewer cards
than the other Unowns…

00:07:53.672 --> 00:07:56.059
Yes, I don’t know.

00:07:56.059 --> 00:07:58.448
I’m really happy with how the collage&nbsp;turned out. 

00:07:58.448 --> 00:08:02.080
And I hope this artistic break&nbsp;
from my usual game development content was&nbsp;&nbsp;

00:08:02.080 --> 00:08:05.760
fun for you. If you have any thoughts about&nbsp;
the project, feel free to leave them in the&nbsp;&nbsp;

00:08:05.760 --> 00:08:10.926
comments or join the discussion on Discord.&nbsp;
Thanks for watching, and see you next time.

