Dice grid from a photo with Python (I) – Logic behind the program
This post is part of the Project: Create a grid of dice from a photo with Python and PIL
Before we code the program, it is necessary to have a general idea about how we will solve the problem that surrounds us. Here, we want to know how can we create a grid of dice parting from a picture, and what must we do to put the correct dice in each part of the picture in such a way that the result resembles the original picture.
Note: In this post, I will only show the logic behind the program and therefore no coding will be done.
In this post, we will analyze:
In the previous template, we can see the coordinates of each dot and the size of the die in its simplest form, something that will matter when we draw it in Python.
Now we know that the size of a die is 7x7 px now we have to divide the picture into subsets of itself, dividing the picture into small squares of 7x7 px.
This is not necessarily a visible process, but one we must follow to analyze the problem, however, if we illustrate it would look similar to the following picture
This picture has a resolution of 637x595, both numbers entirely divisible by 7 (637÷7 = 91, 595÷7 = 85), but what if we get a picture that is not perfectly divided by 7, like 640x597? Then we can not divide it into subsets of 7x7, unless we change the size of the dice or resize the picture, an acceptable process even if it's necessary to modify the ratio of the picture slightly.
The answer is simple: Depending on the brightness or darkness of each subset is the die that will be pasted in the picture: The brightest parts will have a lower number of dots, while the darkest parts will have a greater number of dots.
Fairly simple, isn't it?
Now we have another problem: How are we going to know the amount of white or black in each part of the picture? This is where the value of the bands of the image comes in every pixel
In the RGB color model, we have three bands to represent the amount of Red, Green, and Blue in an area with values going from 0 to 255.
What we need to do is analyze the value of each pixel in the 7x7 area, get the average and replace the area in the picture with the proper die, but we can do this process even easier if we convert the image to grayscale, this way we'll only work with one band of color instead of three.
Now that we are talking only about one band we can declare a formula to find the interval of values that will be replaced by each die:
Where:
We can represent it like this:
And that's all you need to know to start coding the program; you can see the whole process summarized in the following picture:
In the next post, I'll show how to reproduce this process with Python 3.
- The logic behind the program
- Creation of the dice
- Create a grid of dice from a photo
- Design of the GUI
- Document and creation of the stand-alone executable
Introduction
Before we code the program, it is necessary to have a general idea about how we will solve the problem that surrounds us. Here, we want to know how can we create a grid of dice parting from a picture, and what must we do to put the correct dice in each part of the picture in such a way that the result resembles the original picture.
Note: In this post, I will only show the logic behind the program and therefore no coding will be done.
In this post, we will analyze:
- How to draw the dice
- How to divide the picture into subsets
- How to decide which die paste in each part of the picture
Thinking of a template for the die
We are going for the simplest approach to draw the dice: 1 dot per pixel. As the die is a cube and its face a square, the width and height are the same and considering that there must be one row in between each horizontal dot and one column between each vertical dot, we can think of the following template when drawing a die:In the previous template, we can see the coordinates of each dot and the size of the die in its simplest form, something that will matter when we draw it in Python.
Divide the picture into subsets
Now we know that the size of a die is 7x7 px now we have to divide the picture into subsets of itself, dividing the picture into small squares of 7x7 px.
This is not necessarily a visible process, but one we must follow to analyze the problem, however, if we illustrate it would look similar to the following picture
How to decide which die paste in each part of the picture
Until now, we have only defined the size of the dice and analyze the grid of subsets in the picture but now comes the most important part of the program: How to decide which die put in each subset of the picture.The answer is simple: Depending on the brightness or darkness of each subset is the die that will be pasted in the picture: The brightest parts will have a lower number of dots, while the darkest parts will have a greater number of dots.
Fairly simple, isn't it?
Now we have another problem: How are we going to know the amount of white or black in each part of the picture? This is where the value of the bands of the image comes in every pixel
In the RGB color model, we have three bands to represent the amount of Red, Green, and Blue in an area with values going from 0 to 255.
What we need to do is analyze the value of each pixel in the 7x7 area, get the average and replace the area in the picture with the proper die, but we can do this process even easier if we convert the image to grayscale, this way we'll only work with one band of color instead of three.
Now that we are talking only about one band we can declare a formula to find the interval of values that will be replaced by each die:
Where:
- n is the number of dots in the die
- 255 is the max value in the band
- 7 is the total number of dice
And that's all you need to know to start coding the program; you can see the whole process summarized in the following picture:
In the next post, I'll show how to reproduce this process with Python 3.
Comments
Post a Comment