My experiment drawing Mexico flag with thousands pictures

in #python5 years ago (edited)

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.

Sort:  

Thank you for your contribution. This is a nice idea.

  1. I notice that the code is not on an independent repro, instead there are some other irrelevant project(s), this makes it harder to see the changes log in the future.
  2. The code demonstrates well the idea, but it makes it hardly practical for general use. Expecially that not every people knows how to put the image paths into the temporal database.
  3. Also, directories and filenames are hard-coded in the code, you might want to improve the utility by accepting command line options.
  4. You might want to extract the functionalities as a few funtions/modules, so that you can add unit tests.
  5. Almost no comments, you might want to comment more - and in English :)
  6. And please also add more documentation in the README.md
  7. You might want to use Pull Request to group your commits.

Lookingforward to your next contribution.

According to Utopian policies and guidelines, here are two important ones that you will not want to miss:

To be considered for potential reward, Bug Fixes and New Features should be submitted via Pull Requests. The Pull Request should have been merged within the past 14 days.

Updates on Own Projects may be committed directly, without a Pull Request. To be considered for potential reward, commits should not be older than 14 days.

Happy New Year!


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @justyy! Keep up the good work!

hazme una foto curada de mi gato con muchas fotitos :V jajajaja se andaba usando hace uuuuu
te quedó curada

"foto con fotitos" aunque ese método era colorizar las fotos, con mi método se necesitan muchíiiisimas fotos y que coincidan con la tonalidad. 😎

Ingaturroña

Posted using Partiko Android

Congrats man!!! Viva México!😁🇲🇽🌶🌯🌮Feliz Año 2019🥂😉

Posted using Partiko Android

Feliz Año. Gracias por tu comentario. 😁

Invitación - @team-mexico
Canal Discord ¡Te esperamos!

Hola! @gama espero que la hayas pasado bien recibiendo este nuevo año! Te invito a compartir tu celebración en el Concurso de @anomadsoul @blocktrades echa un vistazo aqui:😉 https://steemit.com/steempress/@anomadsoul/contestmy2018welcomingthenewyear-100steeminprizes-mo805y48z7 Buena Suerte! Feliz 2019!😁🍀

Posted using Partiko Android

Congratulations @gama! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 3 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.27
TRX 0.13
JST 0.032
BTC 61763.08
ETH 2899.43
USDT 1.00
SBD 3.49