My experiment drawing Mexico flag with thousands pictures

In February 23th, 2017, I was thinking in program something for giving honor to Mexico flag, since February 24th is the national day of the Mexico flag, "El día de la bandera" in spanish.

Since I like photography, I took around 23k pictures of my gallery, indexed them and make this picture.

16682012_10154778378455862_7162353301908954212_n.jpg

Well, I chose Python language, it's the one I have more experience on. And decided to use "Pillow" library for image processing. The algorithm is like this...

What I did

Step 1. Create a new empty image 1280x1280 in memory. All will be drawn here.

Step 2. Create 3 different lists: green, white, red. This is for indexing images that are mostly those colors.

Step 3. Given a directory I iterate recursively. And save all the files path in a temporal database, so there's no need to walk directory tree everytime.

Step 4. I open each of the files. A counter for each color variation is taken in account: green, white, red.
mexico-flag_py_—_pixteem.jpg

Step 4 and one tenth. To compare each of the colors is like this

  • green pixel when green > blue + 10 and green > red + 10
  • white pixel when red > 190 and green > 190 and blue > 190
  • red pixel when red > blue + 55 and red > g + 55

mexico-flag_py_—_pixteem.jpg

Step 4 and one fifth. After we complete iterating each pixel of the current image, we compare the counters of color similarities. If any of the counters counters are greater than the other two, that image will be considered redish, whiteish or greenish, and will be added respectively to its list, we continue to next image.

mexico-flag_py_—_pixteem.jpg

Step 5. Now we have a list of images that appears to be greenish, redish or whiteish.

Step 6. Iterating a virtual list of 3, where 0 is green, 1 is white and 2 is reddish, I put each of the image corresponding its color given an offset. I show the function.

mexico-flag_py_—_pixteem.jpg

And that's it, i run it

python mexico-flag.py
# I get the image in PNG format. The eagle was added in Photoshop.

You can look at the code here: https://github.com/gamikun/cosas-raras/blob/master/big-picture/mexico-flag.py

Some notes

  • Lot of images are needed so images can be really found as mostly those colors.
  • Small images are preferred for faster processing, I used the thumbnails of Apple Photos gallery.
  • Process is heavy and takes too long.
  • Image indexing is made one time, unless you delete the file db.data to reindex.

Thaks for reading and have a nice day.

Licensed under a Creative Commons Attribution 4.0 International License.

H2
H3
H4
3 columns
2 columns
1 column
11 Comments