-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
91 lines (71 loc) · 2.32 KB
/
helpers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
from pathlib import Path
from PIL import Image
from typing import Union, Any, List, Optional, cast
from typing import List
def psd_name(path: str, message: str):
user_input = input(f"{message}\n")
if user_input:
if directory_exists(path):
for file in os.listdir(path):
if user_input in file:
return file
if not user_input:
if directory_exists(path):
for file in os.listdir(path):
if ".psb" in file or ".psd" in file:
return file
def valid_directory(message: str) -> str:
while True:
try:
path = input(f"{message}\n")
except ValueError:
continue
if not directory_exists(path):
print("Invalid path, try again.")
continue
else:
break
return path
def directory_exists(path: str) -> bool:
if os.path.exists(os.path.dirname(path)):
return True
return False
def decimal_count(count: int) -> str:
if isinstance(count, int):
if count <= 9:
return f"0{count}"
else:
return f"{count}"
def add_underscore_is_missing(pattern: str) -> str:
if not pattern:
return pattern
elif "_" not in pattern[-1:]:
return f"{pattern}_"
return pattern
def convert_width_if_email(width: str) -> str:
if width == "850":
return "Mobile"
elif width == "1280":
return "Desktop"
else:
return width
def image_name_ext(pattern: Any, width: str, count: Any) -> str:
count = decimal_count(count)
return f"{pattern}{width}_{count}.jpg"
def image_name_ext_nonretina(width: str, count: Any) -> str:
count = decimal_count(count)
return f"{width}_{count}_NonRetina.jpg"
def save_image(layer: "PIL.Image.Image", name: str, output):
image = layer.compose()
image.convert("RGB").save(
Path(output).joinpath(name), quality=100, optimize=True, progressive=True
)
def save_image_halfsize(parent, layer: "PIL.Image.Image", name: str, output):
image = layer.compose()
width = int(layer.width / 2)
height = int(layer.height / 2)
img = image.resize((width,height), Image.ANTIALIAS)
img.convert("RGB").save(
Path(output).joinpath(name), quality=100, optimize=True, progressive=True
)