Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Itzik - Last version of the code #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
54 changes: 48 additions & 6 deletions Demo-Code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ def main():
Image.fromarray(rand_pic_np).save("result.png")
print(ball_set)

def pic_generator(yields_num, max_balls, min_diam, max_diam, bg_width, bg_height):
def pic_generator(yields_num: int, max_balls: int, min_diam: int, max_diam: int, bg_width: int, bg_height: int):
"""
:param yields_num: number of yields
:yield: get_random_pic(max_balls, min_diam, max_diam, bg_width, bg_height)
Itzikmilsh marked this conversation as resolved.
Show resolved Hide resolved
"""
for i in range(yields_num):
yield get_random_pic(max_balls, min_diam, max_diam, bg_width, bg_height)

def get_random_pic(max_balls, min_diam, max_diam, bg_width, bg_height):
def get_random_pic(max_balls: int, min_diam: int, max_diam: int, bg_width: int, bg_height: int):
"""
:param max_balls: max amount of balls printed on each picture
:param min_diam: min diameter of each ball
:param max_diam: max diameter of each ball
:param bg_width: width of the background
:param bg_height: height of the background
:return: a numpy array representing a random background from "Demo-BGSet" resized to bg_width x bg_height with ball_count = 0...max_balls random balls from "Demo-BallSet"
resized to a random diameter between min_diam and max_diam (including) printed on the background in random places,
a numpy array of 0...max_balls tuples each representing a ball by it's center coordinates and radius
when the array is sorted in accending order by the sum of its coordinates.
"""
bg_file = random.choice(os.listdir("Demo-BGSet"))
bg_image = Image.open("Demo-BGSet" + os.sep + bg_file)
bg_arr = np.array(bg_image.resize((bg_width, bg_height)))
Expand All @@ -26,7 +41,15 @@ def get_random_pic(max_balls, min_diam, max_diam, bg_width, bg_height):
ball_list.sort(key = lambda item: item[0] + item[1])
Itzikmilsh marked this conversation as resolved.
Show resolved Hide resolved
return (bg_arr, np.array(ball_list))

def randomize_balls(max_balls, min_diam, max_diam, bg_width, bg_height):
def randomize_balls(max_balls: int, min_diam: int, max_diam: int, bg_width: int, bg_height: int):
"""
:param max_balls: max amount of balls printed on each picture
:param min_diam: min diameter of each ball
:param max_diam: max diameter of each ball
:param bg_width: width of the background
:param bg_height: height of the background
:return: a list of ball_count = 0...max_ball tuples containing randomized top left corner coordinates of the ball and it's randomized diameter
"""
ball_count = random.randint(0, max_balls)
tup_list = []
for i in range(ball_count):
Expand All @@ -37,22 +60,41 @@ def randomize_balls(max_balls, min_diam, max_diam, bg_width, bg_height):
tup_list.append(tup)
return tup_list

def create_ball(bg_width, bg_height, max_diam, min_diam):
def create_ball(bg_width: int, bg_height: int, max_diam: int, min_diam: int):
"""
:param min_diam: min diameter of each ball
:param max_diam: max diameter of each ball
:param bg_width: width of the background
:param bg_height: height of the background
:return: a tuple containing the randomized top left corner coordinates and the randomized diameter of the ball
"""
diam = random.randint(min_diam, max_diam)
x = random.randint(0, bg_width - (diam + 1))
y = random.randint(0, bg_height - (diam + 1))
tup = (x, y, diam)
return tup

def is_ball_legal(tup_list, tup):
def is_ball_legal(tup_list: list, tup: tuple):
"""
:param tup_list: list of all balls olready created
:param tup: a new ball
:return: true if the ball does not interrupt other balls, else false
"""
for i in tup_list:
if (((i[0] + i[2] / 2.0) - (tup[0] + tup[2] / 2.0)) * ((i[0] + i[2] / 2.0) - (tup[0] + tup[2] / 2.0)) +
((i[1] + i[2] / 2.0) - (tup[1] + tup[2] / 2.0)) * ((i[1] + i[2] / 2.0) - (tup[1] + tup[2] / 2.0)) <=
((tup[2] + i[2]) / 2.0 + 1) * ((tup[2] + i[2]) / 2.0 + 1)):
return False
return True

def circle_copy_stupid(ball_arr, bg_arr, x_top_left, y_top_left, diameter): # diameter equals to width
def circle_copy_stupid(ball_arr: np.ndarray, bg_arr: np.ndarray, x_top_left: int, y_top_left: int, diameter: int): # diameter equals to width
"""
:param ball_arr: numpy array representing the background picture
:param bg_arr: numpy array representing a ball picture
:param x_top_left: the top left x coordinate of the location the ball is pasted on the background
:param y_top_left: the top left y coordinate of the location the ball is pasted on the background
:param diameter: the diameter of the ball
"""
r = diameter / 2.0
x_center, y_center = int(r - 0.25) + 0.5, int(r - 0.25) + 0.5
for x_counter in range(diameter + 1):
Itzikmilsh marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
6 changes: 5 additions & 1 deletion Demo-toRGB.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from PIL import Image

def main():
"""
This function turns all Ball picture and BG pictures to RGB (24 bit per pixel).
If the files are already RGB, the functions does nothing.
"""
ball_files, bg_files = os.listdir("Demo-BallSet"), os.listdir("Demo-BGSet")
for ball in (ball_files):
rgb_ball_image = Image.open("Demo-BallSet" + os.sep + ball).convert('RGB')
Itzikmilsh marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -12,6 +16,6 @@ def main():
rgb_bg_image = Image.open("Demo-BGSet" + os.sep + bg).convert('RGB')
np_bg = np.array(rgb_bg_image)
Image.fromarray(np_bg).save("Demo-BGSet" + os.sep + bg)

if __name__ == "__main__":
main()