-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataAugmentation.py
More file actions
67 lines (52 loc) · 2.76 KB
/
Copy pathDataAugmentation.py
File metadata and controls
67 lines (52 loc) · 2.76 KB
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
import os
from PIL import Image
import random
batches = os.listdir("Data/Raw")
# background_use_num = 1
# head_use_num = 1
size = 64
x = 0
y = 0
im_num = 0
# Use https://online.photoscissors.com/ to cut out head
def generating_waldos(use_background=True):
background_use_num = 2
head_use_num = 2
size = 64
im_num = 0
# Getting head
for head_batch in os.listdir("Data/Cleaning/OnlyWaldoHeads"):
for head_name in os.listdir("Data/Cleaning/OnlyWaldoHeads/" + head_batch):
for _ in range(head_use_num):
for back_batch in os.listdir("Data/Cleaning/ClearedWaldo"):
for back_name in os.listdir("Data/Cleaning/ClearedWaldo/" + back_batch):
for _ in range(background_use_num):
# Rotate image
if random.randint(0, 9) < 5:
num = random.randint(-15, 15)
foreground = Image.open(
"Data/Cleaning/OnlyWaldoHeads/" + head_batch + "/" + head_name).rotate(
num)
else:
foreground = Image.open("Data/Cleaning/OnlyWaldoHeads/" + head_batch + "/" + head_name)
if random.randint(0, 9) < 7:
scale = random.uniform(0.8, 1.5)
w, h = foreground.size
foreground = foreground.resize((int(w * scale), int(h * scale)), Image.ANTIALIAS)
if use_background:
background = Image.open("Data/Cleaning/ClearedWaldo/" + back_batch + "/" + back_name)
else:
background = Image.open("Data/Cleaning/black_background.jpg")
# Crop background and place foreground on top
bck_w, bck_h = background.size
frg_w, frg_h = foreground.size
bck_x = random.randint(0, bck_w - size)
bck_y = random.randint(0, bck_h - size)
frg_x = random.randint(0, 64 - frg_w)
frg_y = random.randint(0, 64 - frg_h)
cropped = background.crop((bck_x, bck_y, bck_x + size, bck_y + size))
cropped.save("Data/NotWaldo/n" + str(im_num) + ".png")
cropped.paste(foreground, (frg_x, frg_y), foreground)
cropped.save("Data/Waldo/" + str(im_num)+str(use_background) + ".png")
im_num += 1
generating_waldos(use_background=True)