-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff21d52
commit b66c345
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import re | ||
|
||
from dotenv import load_dotenv | ||
|
||
# Loads environment variables | ||
load_dotenv() | ||
|
||
# Access the environment variables | ||
in_jpg_path = os.getenv("IN_JPG_PATH") | ||
|
||
root_folder = str(in_jpg_path) | ||
|
||
for root, _dirs, files in os.walk(root_folder): | ||
for filename in files: | ||
# split the filename into base and extension | ||
base, ext = os.path.splitext(filename) | ||
# replace spaces with underscores in the base filename | ||
base = base.replace(" ", "_") | ||
# remove non-alphanumeric characters from base filename | ||
base = re.sub(r"[^\w\s]", "", base) | ||
# join the modified base filename and original extension | ||
new_filename = base + ext | ||
# rename file | ||
os.rename(os.path.join(root, filename), os.path.join(root, new_filename)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
|
||
from dotenv import load_dotenv | ||
from PIL import Image | ||
|
||
# Loads environment variables | ||
load_dotenv() | ||
|
||
# Access the environment variables | ||
in_jpg_path = str(os.getenv("IN_JPG_PATH")) | ||
|
||
|
||
def compress_image(filepath: str) -> None: | ||
max_size = 5000000 # 5MB | ||
img = Image.open(filepath) | ||
if os.path.getsize(filepath) <= max_size: | ||
print(f"{filepath} is already small enough.") | ||
return | ||
else: | ||
print(f"Compressing {filepath}...") | ||
img.save(filepath, optimize=True, quality=80) | ||
while os.path.getsize(filepath) > max_size: | ||
img.save(filepath, optimize=True, quality=img.info["quality"] - 5) | ||
print(f"{filepath} compressed successfully.") | ||
|
||
|
||
input_folder = in_jpg_path | ||
|
||
for root, _dirs, files in os.walk(input_folder): | ||
for filename in files: | ||
if filename.endswith(".jpg"): | ||
filepath = os.path.join(root, filename) | ||
compress_image(filepath) |