Legofy and Sliding Images

I have subscribed to the Pycoder’s Weekly Newsletter, where I have stumbled upon JuanPotato‘s python program Legofy. Legofy is a program, that can take an image and convert it to look like it was made from Lego blocks. This intrigued me, because I couldn’t imagine what he meant by that statement without looking at his example and finally trying his program out myself.

I also learned a new way of downloading projects from github and installing them directly with pip. You just have to add a “git+” in front of the URL to the git repository. That means, that your command for downloading Legofy would look as following:

 pip install git+https://github.com/JuanPotato/Legofy.git

This works like every other pip install. Which is really helpful to know for the future and is a great time-saver.

I then wrote a quick and dirty script, that takes the file path to an image as a command line argument and converts this to a Lego image. I only check if the number of command line arguments match and didn’t consider checking if the image exists before calling legofy.main(). For one legofy.main() has its own error message when the given image name doesn’t exist and also since this should only be a short example script:

import legofy
"""Using legofy to convert images given in cmd line"""

import legofy
import os
import sys

"""Check number of cmd line args"""
if len(sys.argv) != 2:
	print("Wrong Arguments --- Your command should have the following structure: python test.py <file path to image>")
	sys.exit()

here = os.path.abspath(os.path.dirname(__file__))
legofy.main(os.path.join(here, sys.argv[1]), os.path.join(here, "brick.png"))

You also have to have “brick.png”, which can be found in JuanPotato’s repository in the folder legofy/bricks/brick.png, in the same directory as the above python code to make the program work (or you change the code to match your file path to brick.png).

I have tried this out on different images and found the results really cool looking. I have added a slider to compare the two image versions side-by-side (inspired by this post). By pulling the white arrow under the image you can slide the partition showing the original image and the Lego version of the image. Here are some examples:

(These are images off flickr, that were published under the Creative Commons license)

(source)

(source)

By displaying the images with the slider, you can make a game out of it to guess what the Lego image is trying to depict. Thank you to JuanPotato for this fun little program.

Have a nice weekend!

The post Legofy and Sliding Images appeared first on Rather Read.