inteligentny-traktor/NN/Generator.py
2023-06-15 14:03:24 +02:00

83 lines
4.0 KiB
Python

from PIL import Image
import random
plants = [[], [], []]
plants[0].append(Image.open("w1.png"))
plants[0].append(Image.open("w2.png"))
plants[0].append(Image.open("w3.png"))
plants[1].append(Image.open("c1.png"))
plants[1].append(Image.open("c2.png"))
plants[1].append(Image.open("c3.png"))
plants[2].append(Image.open("ca1.png"))
plants[2].append(Image.open("ca2.png"))
plants[2].append(Image.open("ca3.png"))
b = [Image.open("b1.png").convert('RGBA'), Image.open("b2.png").convert('RGBA'), Image.open("b3.png").convert('RGBA')]
def generate(water, fertilizer, plantf):
if water == 1:
new_im = Image.new('RGB', (100, 100),
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10), 40 + random.randint(-10, 10)))
tmp = plants[plantf][random.randint(0, 2)].resize(
(25 + random.randint(-10, 25), 25 + random.randint(-10, 25))).rotate(random.randint(0, 359))
new_im.paste(tmp, (random.randint(0, 50), random.randint(0, 50)), tmp)
if fertilizer:
tmp = b[random.randint(0, 2)].resize(
(20 + random.randint(0, 25), 20 + random.randint(0, 25))).rotate(random.randint(0, 359))
new_im.paste(tmp, (random.randint(25, 75), random.randint(25, 75)), tmp)
else:
if fertilizer:
new_im = Image.new('RGB', (100, 100),
(
50 + random.randint(-10, 10), 25 + random.randint(-10, 10),
0 + random.randint(-10, 10)))
tmp = plants[plantf][random.randint(0, 2)].resize(
(25 + random.randint(-10, 25), 25 + random.randint(-10, 25))).rotate(random.randint(0, 359))
new_im.paste(tmp, (random.randint(0, 50), random.randint(0, 50)), tmp)
tmp = b[random.randint(0, 2)].resize(
(20 + random.randint(0, 25), 20 + random.randint(0, 25))).rotate(random.randint(0, 359))
new_im.paste(tmp, (random.randint(25, 75), random.randint(25, 75)), tmp)
else:
if random.randint(0, 1) == 1:
new_im = Image.new('RGB', (100, 100),
(50 + random.randint(-10, 10), 25 + random.randint(-10, 10),
0 + random.randint(-10, 10)))
else:
new_im = Image.new('RGB', (100, 100),
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10),
40 + random.randint(-10, 10)))
if random.randint(0, 1) == 1: # big
tmp = plants[plantf][random.randint(0, 2)].resize(
(75 + random.randint(-10, 25), 75 + random.randint(-10, 25))).rotate(random.randint(0, 359))
new_im.paste(tmp, (random.randint(0, 15), random.randint(0, 15)), tmp)
else:
tmp = plants[plantf][random.randint(0, 2)].resize(
(random.randint(10, 80), random.randint(10, 80))).rotate(random.randint(0, 359))
datas = tmp.getdata()
new_image_data = []
for item in datas:
# change all white (also shades of whites) pixels to yellow
if item[0] in list(range(190, 256)):
new_image_data.append(
(random.randint(0, 10), 255 + random.randint(-150, 0), random.randint(0, 10)))
else:
new_image_data.append(item)
# update image data
tmp.putdata(new_image_data)
new_im.paste(tmp, (random.randint(0, 30), random.randint(0, 30)), tmp)
return new_im
for x in range(0, 1000):
generate(0, 0, random.randint(0, 2)).save('datasets/00/' + str(x) + '.png')
for x in range(0, 1000):
generate(1, 0, random.randint(0, 2)).save('datasets/10/' + str(x) + '.png')
for x in range(0, 1000):
generate(0, 1, random.randint(0, 2)).save('datasets/01/' + str(x) + '.png')
for x in range(0, 1000):
generate(1, 1, random.randint(0, 2)).save('datasets/11/' + str(x) + '.png')