Repository navigation
Expand file tree
/
Copy path_common.py
More file actions
80 lines (62 loc) · 2.11 KB
/
Copy path_common.py
File metadata and controls
80 lines (62 loc) · 2.11 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import mtsound
import os
import sys
from PIL import Image
from mtlogger import logger
from mtprompt import Prompt
from collections.abc import Callable
from _constants import FOLDER_IMAGE_FILENAME, FOLDER_IMAGE_SIZE
def process_parent_folder(
process_folder: Callable,
):
if len(sys.argv) > 1:
parent_folder = sys.argv[1]
else:
parent_folder = Prompt.dir(
'Enter the path to the parent folder containing the folders you want to generate icons for.\nLeave empty to provide and process a single folder instead',
optional=True,
)
if not parent_folder:
target_folder = Prompt.dir(
'Enter the path to the specific folder you want to generate an icon for'
)
logger.log()
process_folder(target_folder)
logger.log('Generating cover images...')
for root, dirs, _ in os.walk(parent_folder):
for dir_name in dirs:
item_path = os.path.join(root, dir_name)
if os.path.exists(os.path.join(item_path, FOLDER_IMAGE_FILENAME)):
logger.trace(
f' [{dir_name}] "{FOLDER_IMAGE_FILENAME}" already exists.'
)
continue
process_folder(item_path)
mtsound.notify()
logger.log('Finished generating cover images.')
def resize_image(
img: Image.Image,
w: int,
h: int,
):
return img.resize((w, h), Image.LANCZOS)
def save_resized_image(
img: Image.Image,
folder_path: str,
):
original_width, original_height = img.size
if original_width > FOLDER_IMAGE_SIZE and original_height > FOLDER_IMAGE_SIZE:
if original_width < original_height:
new_width = FOLDER_IMAGE_SIZE
new_height = int((FOLDER_IMAGE_SIZE / original_width) * original_height)
else:
new_height = FOLDER_IMAGE_SIZE
new_width = int((FOLDER_IMAGE_SIZE / original_height) * original_width)
img = resize_image(img, new_width, new_height)
if img.mode == 'RGBA':
background = Image.new('RGBA', img.size, (255, 255, 255, 0))
background.paste(img, (0, 0), img)
img = background
output_file_path = os.path.join(folder_path, FOLDER_IMAGE_FILENAME)
img.save(output_file_path)
logger.debug(f'Saved {output_file_path}.\n')