diff --git a/config.json b/config.json new file mode 100644 index 0000000..326b197 --- /dev/null +++ b/config.json @@ -0,0 +1,9 @@ +{ + "file_type_extensions": { + "raw": [".NEF"], + "large": [".png"], + "small": [".jpeg"], + "edits": [".pp3"], + "desq": [".md"] + } +} \ No newline at end of file diff --git a/example/img1/test.html b/example/img1/test.html deleted file mode 100644 index d766beb..0000000 --- a/example/img1/test.html +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - Dropdown Button - - - - - - - - - - - - - - - diff --git a/src/filename.py b/src/filename.py new file mode 100644 index 0000000..cf4a0b6 --- /dev/null +++ b/src/filename.py @@ -0,0 +1,5 @@ +LARGE = "*.png" +SMALL = "*.jpeg" +RAW = "*.nef" +EDIT = "*.pp3" +DESC = "*.md" \ No newline at end of file diff --git a/src/main.py b/src/main.py index 0652ca6..5896f27 100644 --- a/src/main.py +++ b/src/main.py @@ -1,14 +1,103 @@ import sys import help -import PIL +import rawpy +import imageio +import os +import shutil +import json +from PIL import Image +from jinja2 import Environment, FileSystemLoader +import subprocess + +with open('config.json', 'r') as file: + config: dict = json.load(file) + +# Initialiser l'environnement avec un dossier de templates +env = Environment(loader=FileSystemLoader('src/templates')) + +template = env.get_template('index.html') + +def get_files_recursive(directory): + file_paths = [] + for root, dirs, files in os.walk(directory): + for file in files: + file_paths.append(os.path.join(root, file)) + return file_paths + + +def move_files(files: list[str], input_path, output_path): + for key, type_file_extensions in config["file_type_extensions"].items(): + for file in files: + for type_file_extension in type_file_extensions: + if file.endswith(type_file_extension): + path = file.replace(input_path, output_path)[:-len(type_file_extension)] + if not os.path.exists(path): + if (key not in ["raw", "large", "small"]): + continue + os.makedirs(path) + shutil.copy2(file, path) + +def nef_to_jpeg(path:str): + with rawpy.imread(path) as raw: + rgb = raw.postprocess() + imageio.imwrite(path.replace('.NEF', '.jpeg'), rgb) + +def png_to_jpeg(path: str): + im = Image.open(path) + rgb_im = im.convert('RGB') + rgb_im.save(path.replace('.nef', '.jpeg')) + + +def generate_missing_images(raws: list[str], large: list[str], small: list[str]): + if len(small) == 0: + if (len(large) == 0): + nef_to_jpeg(raws[0]) + return + png_to_jpeg(large[0]) + return + +def get_exif(raws: list[str]): + if (len(raws) == 0): + return None + return str(subprocess.check_output(["exiftool", f"{raws[0]}"])).replace("\\n", "\n") + + +def generate_page(output_path: str): + for folder in os.listdir(output_path): + path = os.path.join(output_path, folder) + if (os.path.isdir(path)): + files: list[str] = [os.path.join(path, file) for file in os.listdir(path) if os.path.isfile(os.path.join(path, file))] + + raws = [file for ext in config["file_type_extensions"]["raw"] for file in files if file.endswith(ext)] + larges = [file for ext in config["file_type_extensions"]["large"] for file in files if file.endswith(ext)] + smalls = [file for ext in config["file_type_extensions"]["small"] for file in files if file.endswith(ext)] + + generate_missing_images(raws, larges, smalls) + + exif = get_exif(raws) + raw = os.path.basename(raws[0]) if len(raws) > 0 else None + large = os.path.basename(larges[0]) if len(larges) > 0 else None + small = os.path.basename(smalls[0]) if len(smalls) > 0 else None + + html_rendered = template.render(exif=exif, raw=raw, large=large, small=small) + + with open(os.path.join(path, f"{folder}.html"), "w") as f: + f.write(html_rendered) def main(): - if len(sys.argv) != 2: + if len(sys.argv) < 2: help.display(sys.argv[0]) return - - + + input_path = sys.argv[1] + output_path = "output/" if (len(sys.argv) == 2) else sys.argv[2] + "/" + + files: list[str] = get_files_recursive(input_path) + + move_files(files, input_path, output_path) + + generate_page(output_path) if (__name__ == "__main__"): main() \ No newline at end of file diff --git a/src/template/index.html b/src/template/index.html deleted file mode 100644 index 7ed9f32..0000000 --- a/src/template/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..219b54a --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,46 @@ + + + + + + + + \ + {% if large or raw %} +
+ {% if large %} + + +

large

+
+ {% endif %} + {% if raw %} + + +

raw

+
+ {% endif %} +
+ {% endif %} + {% if desc %} +
+
+ {% endif %} + {% if exif %} +
+ + + + + + +
+ {% endif %} + + \ No newline at end of file