-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.py
66 lines (50 loc) · 1.74 KB
/
flags.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
# Python 3.8.6
# 8.0.1
from PIL import Image
# 1.1.0
import pyTGA
import os
def make_folder(path: str) -> None:
try:
os.mkdir(path)
except FileExistsError:
pass
def save_to_tga(input_image: Image.Image, path: str) -> None:
pixels = list(input_image.getdata())
width, height = input_image.size
pixels = [pixels[i * width:(i + 1) * width] for i in range(height)]
pixels.reverse()
image = pyTGA.Image(data=pixels)
image.set_first_pixel_destination('bl')
image.save(path)
def make_flags(path_to_image: str, tag: str, output_dir: str = 'output',
large_width: int = 82, large_height: int = 52,
medium_width: int = 41, medium_height: int = 26,
small_width: int = 10, small_height: int = 7) -> None:
img_base = Image.open(path_to_image).convert('RGBA')
# Make large flag tga
make_folder(f'{output_dir}')
img_large = img_base.resize((large_width, large_height))
save_to_tga(img_large, f'{output_dir}/{tag}')
# Make medium flag tga
make_folder(f'{output_dir}/medium')
img_med = img_base.resize((medium_width, medium_height))
save_to_tga(img_med, f'{output_dir}/medium/{tag}')
# Make small flag tga
make_folder(f'{output_dir}/small')
img_small = img_base.resize((small_width, small_height))
save_to_tga(img_small, f'{output_dir}/small/{tag}')
def main():
make_folder('input')
files = os.listdir('input')
count = 0
length = len(files)
for file in files:
if '.png' in file:
tag_ = file.split('.')[0]
make_flags(f'input/{file}', tag_)
count += 1
number = round((count * 100) / length)
print(f"\r{number}%", end="\r")
if __name__ == '__main__':
main()