-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_common.py
More file actions
62 lines (46 loc) · 2.09 KB
/
_common.py
File metadata and controls
62 lines (46 loc) · 2.09 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
import os
import winsound
from PIL import Image
from mtlogger import logger
FOLDER_IMAGE_FILENAME = 'folder.jpg'
FOLDER_IMAGE_SIZE = 256
def process_parent_folder(process_folder):
parent_folder = input('Enter the path to the parent folder containing the folders you want to generate icons for.\nLeave empty instead to provide and process a single folder instead.\nPARENT_FOLDER: ').strip(' "\'')
if parent_folder == '':
target_folder = input('\nEnter the path to the specific folder you want to generate an icon for:\nFOLDER: ')
if not os.path.isdir(target_folder):
logger.error(f'The specified path "{target_folder}" is not a directory.')
return
logger.log()
process_folder(target_folder)
else:
if not os.path.isdir(parent_folder):
logger.error(f'The specified path "{parent_folder}" is not a directory.')
return
logger.log()
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.debug(f'Skipping "{item_path}". "{FOLDER_IMAGE_FILENAME}" is already contained within.')
continue
process_folder(item_path)
winsound.MessageBeep(winsound.MB_ICONASTERISK)
logger.log(f'Finished generating icons.')
def save_resized_image(img, folder_path):
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 = img.resize((new_width, new_height), Image.LANCZOS)
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')