-
Notifications
You must be signed in to change notification settings - Fork 0
/
Formalization.py
47 lines (34 loc) · 1.37 KB
/
Formalization.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
import os
from PIL import Image, ImageOps
import numpy as np
import time
input_directory = "Classified_image"
output_directory = "Formalization_image"
path, dirs, files = next(os.walk(f"{input_directory}"))
if not os.path.isdir(f"{output_directory}"):
os.mkdir(f"{output_directory}")
for dir in dirs:
path, dirs2, files = next(os.walk(f"{input_directory}/{dir}"))
for file in files:
image = Image.open(f"{input_directory}/{dir}/{file}")
x, y = image.size
if x >= y :
new_size = x
x_offset = 0
y_offset = int((x-y)/2)
elif y > x:
new_size = y
x_offset = int((y-x) / 2)
y_offset = 0
background_color = "white"
new_image = Image.new("RGBA", (new_size, new_size), background_color)
new_image.paste(image, (x_offset, y_offset))
new_image = ImageOps.fit(new_image, (204,204), Image.ANTIALIAS)
base_image = Image.new("RGBA", (224, 224), background_color)
base_image.paste(new_image, (10, 10))
new_image = base_image
new_image = new_image.convert("RGB")
if not os.path.isdir(f"{output_directory}/{dir}"):
os.mkdir(f"{output_directory}/{dir}")
print(f"{output_directory}/{dir}/{file}")
new_image.save(f"{output_directory}/{dir}/{file}")