-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrees.py
67 lines (53 loc) · 2.62 KB
/
Trees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import numpy as np
import cv2 as cv
import random
import time as sleep
# general parameters
width = 900
height = 600
n_trees = 10
ground_level = height-100
# blank image
bg = np.zeros((height, width, 3), dtype=np.uint8)
# draw background
cv.rectangle(bg,(width,0), (0, ground_level), (255,225,95), -1)
# ***********************
class Tree:
def __init__(self, image):
self.img = image
self.loc = int(np.random.choice(range(width), 1))
self.ht = int(np.random.choice(range(200,400), 1))
self.radius = 50
self.scale = np.random.choice(np.linspace(0.5,2, num=8), 1)
def generate_colours(self):
green = (0, random.randint(130,200),0)
light_green = (35, random.randint(200,250),35)
brown = random.choice([(2,30,85), (5,55,120), (0,70,140)])
return green, light_green, brown
def draw(self):
small_radius = int(self.radius*self.scale-20*self.scale)
green, light_green, brown = self.generate_colours()
# leafs - shadows
cv.circle(self.img, (self.loc,ground_level-self.ht), int(self.radius*self.scale), green, -1)
cv.circle(self.img, (self.loc-int(45*self.scale),ground_level-self.ht+small_radius), small_radius , green, -1)
cv.circle(self.img, (self.loc+int(45*self.scale),ground_level-self.ht+small_radius), small_radius, green, -1)
# trunk
cv.line(self.img, (self.loc,ground_level),(self.loc,ground_level-self.ht), brown, int(20*self.scale))
cv.line(self.img, (self.loc,ground_level-self.ht+int(75*self.scale)),(self.loc+int(45*self.scale),ground_level-self.ht+small_radius), brown, int(5*self.scale))
cv.line(self.img, (self.loc,ground_level-self.ht+int(75*self.scale)),(self.loc-int(45*self.scale),ground_level-self.ht+small_radius), brown, int(5*self.scale))
# leafs - highlights
cv.circle(self.img, (self.loc,ground_level-self.ht), int(self.radius*self.scale-10*self.scale), light_green, -1)
cv.circle(self.img, (self.loc-int(45*self.scale),ground_level-self.ht+small_radius), small_radius-int(10*self.scale) , light_green, -1)
cv.circle(self.img, (self.loc+int(45*self.scale),ground_level-self.ht+small_radius), small_radius-int(10*self.scale), light_green, -1)
cv.rectangle(bg,(width, ground_level), (0, height), green, -1)
return self.img
# ***********************
#display image
for i in range(n_trees):
img = Tree(bg).draw()
cv.imshow('forest of objects', img)
if cv.waitKey(0) & 0xff ==ord('c'):
break
cv.destroyAllWindows()
# cv.waitKey(0)
# cv.destroyAllWindows()