-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagecleaner.py
77 lines (66 loc) · 2.67 KB
/
imagecleaner.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
import sys, os, re, platform, subprocess
from PIL import Image as im
from argparse import ArgumentParser as argparser
# width in pixels
MAX_WIDTH = 720
# filetype
OUTPUT_FILETYPE = "webp"
IMG_FM = (".tif", ".tiff", ".jpg", ".jpeg", ".gif", ".png", ".eps",
".raw", ".cr2", ".nef", ".orf", ".sr2", ".bmp", ".ppm", ".heif", ".webp", "avif")
def main():
if len(sys.argv) < 2:
print("invalid")
return
file_or_directory = sys.argv[1]
if os.path.isfile(file_or_directory):
handle_file(file_or_directory)
elif os.path.isdir(file_or_directory):
handle_directory(file_or_directory)
else:
print("invalid")
def handle_file(filepath):
if verify_file_is_image(filepath):
process_image(filepath)
else:
print("file " + filepath + " is not an image, ignoring")
return
def handle_directory(dirpath):
for entry in os.listdir(dirpath):
if os.path.isdir(dirpath + entry):
print(dirpath + entry + " is subdirectory, ignoring")
else:
handle_file(dirpath + entry)
return
def verify_file_is_image(filepath):
# note that this isnt a bulletproof way to check a file is _actually_ an
# image, but pil can figure out the filetype for us if its been misattributed
# in the file metadata - this just stops us from wasting time trying to
# process markdown files or js code, but also means we're not relying on some
# stupid fucking package solution for an extremely simple problem
return os.path.splitext(filepath)[1] in IMG_FM
def compress_png(filepath):
if platform.system() == "Windows":
subprocess.run(["./optipng.exe", "-o7", filepath])
else:
print("booyah")
# do linux compression
def process_image(filepath):
print("processing " + filepath)
with im.open(filepath) as current_image:
if (current_image.width > MAX_WIDTH):
print("> width is " + str(current_image.width) + ", resizing to " + str(MAX_WIDTH) + "px")
width_percent = MAX_WIDTH / float(current_image.width)
new_height = int((float(current_image.height) * float(width_percent)))
current_image = current_image.resize((MAX_WIDTH, new_height), im.Resampling.LANCZOS)
if current_image.mode in ("RGBA", "P"):
current_image = current_image.convert("RGB")
filename_slug = slugify(os.path.splitext(os.path.basename(filepath))[0])
current_image.save(os.path.join(os.path.dirname(filepath), (filename_slug + "." + OUTPUT_FILETYPE)), quality=90)
return
def slugify(s: str) -> str:
s_after_basic_replacement = re.sub("[^a-zA-Z0-9]", "-", s)
s_with_no_continues_dash = re.sub("[-]+", "-", s_after_basic_replacement)
s_with_no_ending_dash = re.sub("-$", "", s_with_no_continues_dash)
return s_with_no_ending_dash
if __name__ == "__main__":
main()