Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions watermark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ Par David Resin, ESN EPF Lausanne, 2021-2023

## Avant l'utilisation

1. Installer Python 3
1. Installer Python 3.9
2. Exécuter `pip install -r requirements.txt`

## Pour utiliser
## Pour utiliser

1. Insérer les images à traiter dans le dossier `input/`.
2. Exécuter le script avec la commande suivante :
```bash
python watermark.py
```
3. Les images traitées seront sauvegardées dans le dossier `output/`. Les images invalides seront déplacées dans le dossier `invalid/`.

> [!NOTE]
> Les images déposées dans le dossier `input/` peuvent être organisées en sous-dossiers. La structure des dossiers sera préservée dans les dossiers `output/` et `invalid/`.
2 changes: 1 addition & 1 deletion watermark/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .file_operations import *
from .image_manipulation import *
from .others import *
from .others import *
62 changes: 35 additions & 27 deletions watermark/helpers/file_operations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Default libraries
import os
import shutil
from pathlib import Path

Expand All @@ -10,24 +11,24 @@
from helpers.image_manipulation import tilt_img


OTHER_EXTS = ('.jpg', '.png', '.jpeg', '.ico', '.webp')
HEI_EXTS = ('.heic', '.heif')
RAWPY_EXTS = ('.nef',)
OTHER_EXTS = (".jpg", ".png", ".jpeg", ".ico", ".webp")
HEI_EXTS = (".heic", ".heif")
RAWPY_EXTS = (".nef",)
IMG_EXTS = OTHER_EXTS + HEI_EXTS + RAWPY_EXTS

IGNORE_EXTS = ('.ds_store')
IGNORE_EXTS = ".ds_store"

INVALID_COUNT = 0


# Glob all filenames in a given path with a given pattern, but exclude the patterns in the exclusion list
def glob_all_except(path, base_pattern="*", excluded_patterns=[]):
matches = set(path.glob(base_pattern))
matches = set(path.glob(base_pattern))

for pattern in excluded_patterns:
matches = matches - set(path.glob(pattern))
for pattern in excluded_patterns:
matches = matches - set(path.glob(pattern))

return list(matches)
return list(matches)


def extension_match(image_path, extension_list):
Expand All @@ -36,26 +37,26 @@ def extension_match(image_path, extension_list):

# Flush the output directory
def flush_output(path_out: Path, exts: tuple[str]) -> None:
for deletion_candidate in path_out.iterdir():
if extension_match(deletion_candidate, exts):
deletion_candidate.unlink()
for deletion_candidate in path_out.iterdir():
if extension_match(deletion_candidate, exts):
deletion_candidate.unlink()


# Move an invalid picture out
def invalidate_path(image_path, path_invalid):
global INVALID_COUNT
shutil.move(image_path, path_invalid)
INVALID_COUNT += 1
global INVALID_COUNT
shutil.move(image_path, path_invalid)
INVALID_COUNT += 1


# Create a directory if it is missing
def create_dir_if_missing(dir_path):
try:
dir_path.mkdir()
except FileExistsError:
return True
try:
dir_path.mkdir()
except FileExistsError:
return True

return False
return False


def open_rawpy_image(image_path):
Expand All @@ -73,11 +74,11 @@ def open_hei_image(image_path):

def universal_load_image(image_path):
image = None
flag = 'img'
flag = "img"
is_hei = False

if extension_match(image_path, IGNORE_EXTS):
flag = 'ignore'
flag = "ignore"
elif extension_match(image_path, RAWPY_EXTS):
image = open_rawpy_image(image_path)
elif extension_match(image_path, HEI_EXTS):
Expand All @@ -86,34 +87,41 @@ def universal_load_image(image_path):
elif extension_match(image_path, OTHER_EXTS):
image = Image.open(image_path)
else:
flag = 'invalid'
flag = "invalid"

return image, flag, is_hei


def attempt_open_image_attempt_tilt(image):
# TODO : Implement no-tilt option in CLI arguments
# For non-HEI file types, try to re-orient the picture if it is allowed and orientation data is available
# For non-HEI file types, try to re-orient the picture if it is allowed and orientation data is available
try:
# TODO : Potentiellement getexif existe partout alors que _getexif pas
image._getexif()
image = tilt_img(image)
except AttributeError:
# TODO : Setting for what to do when image can't be rotated
pass

return image


def attempt_open_image(image_path, path_invalid, attempt_rotate):
image, flag, is_hei = universal_load_image(image_path)

if flag == 'ignore':
if flag == "ignore":
return None
elif flag == 'invalid':
invalidate_path(image_path, path_inv)
elif flag == "invalid":
invalidate_path(image_path, path_invalid)

if not is_hei and attempt_rotate:
image = attempt_open_image_attempt_tilt(image)

return image


def scandir(dirname):
subfolders = [f.path for f in os.scandir(dirname) if f.is_dir()]
for dirname in list(subfolders):
subfolders.extend(scandir(dirname))
return subfolders
Loading