Skip to content

Commit

Permalink
pictures renamer and resizer
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardbruelhart committed Nov 18, 2024
1 parent ff21d52 commit b66c345
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
94 changes: 93 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pandas = "^2.2.3"
geopandas = "0.14.4"
pandas-stubs = "2.2.2.240807"
fiona = "^1.10.1"
pillow = "^11.0.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.2.0"
Expand Down
3 changes: 3 additions & 0 deletions qfieldcloud_fetcher/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ run_script "db_updater"

# Run directus link maker
run_script "directus_link_maker"

# Run pictures renamer
run_script "pictures_renamer"
27 changes: 27 additions & 0 deletions qfieldcloud_fetcher/pictures_renamer.py
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))
35 changes: 35 additions & 0 deletions qfieldcloud_fetcher/pictures_resizer.py
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)

0 comments on commit b66c345

Please sign in to comment.