simplify bozocod
This commit is contained in:
parent
0b1e2479c1
commit
307c5c4d82
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Jinja2==3.1.6
|
||||
MarkupSafe==3.0.2
|
||||
progress==1.6
|
@ -1,5 +0,0 @@
|
||||
LARGE = "*.png"
|
||||
SMALL = "*.jpeg"
|
||||
RAW = "*.nef"
|
||||
EDIT = "*.pp3"
|
||||
DESC = "*.md"
|
@ -1,4 +0,0 @@
|
||||
|
||||
|
||||
def display(executable: str):
|
||||
print(f"{executable} {{Path of your pictures}} {{Path of the site}}")
|
144
src/main.py
144
src/main.py
@ -1,113 +1,55 @@
|
||||
import sys
|
||||
import help
|
||||
import rawpy
|
||||
import imageio
|
||||
import os
|
||||
import shutil
|
||||
import json
|
||||
from PIL import Image
|
||||
from progress.bar import Bar
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
import subprocess
|
||||
|
||||
with open('config.json', 'r') as file:
|
||||
config: dict = json.load(file)
|
||||
def argument_parsing():
|
||||
if (len(sys.argv) < 2):
|
||||
print("error: missing argument", file=sys.stderr)
|
||||
exit(1)
|
||||
return sys.argv[1]
|
||||
|
||||
# Initialiser l'environnement avec un dossier de templates
|
||||
env = Environment(loader=FileSystemLoader('src/templates'))
|
||||
def get_exif(files, raw):
|
||||
exifs: list[str] = [file for file in files if file.endswith(".exif")]
|
||||
exif: str
|
||||
if len(exifs) > 0:
|
||||
exif = exifs[0]
|
||||
elif (raw is not None):
|
||||
exif = raw[:-3] + "exif"
|
||||
if os.system(f"exiftool {raw} > {exif}"):
|
||||
os.remove(exif)
|
||||
exif = None
|
||||
else:
|
||||
exif = None
|
||||
return exif
|
||||
|
||||
template = env.get_template('page.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):
|
||||
folders = os.listdir(output_path)
|
||||
if "general" in folders:
|
||||
folders.remove("general")
|
||||
for folder in folders:
|
||||
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)]
|
||||
|
||||
print(folder)
|
||||
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
|
||||
|
||||
print(f"Generate {folder} html")
|
||||
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 get_images(files):
|
||||
images_datas: list[str] = []
|
||||
images: list[str] = [file for file in files if os.path.isfile(file) and file.endswith(".png")]
|
||||
for image in images:
|
||||
export_data_file: str = image + ".out.pp3"
|
||||
if (os.path.exists(export_data_file) and os.path.isfile(export_data_file)):
|
||||
images_datas.append((image, export_data_file))
|
||||
images_datas.append((image,))
|
||||
return images_datas
|
||||
|
||||
|
||||
def main():
|
||||
site_path: str = argument_parsing()
|
||||
folders: list[str] = [os.path.join(site_path, f) for f in os.listdir(site_path)]
|
||||
folders = [file for file in folders if os.path.isdir(file)]
|
||||
with Bar("generating...", max=len(folders)) as bar:
|
||||
for folder in folders:
|
||||
files: list[str] = [os.path.join(folder, file) for file in os.listdir(folder)]
|
||||
images: list[str] = get_images(files)
|
||||
raws: list[str] = [file for file in files if file.endswith(".NEF")]
|
||||
raw: str = raws[0] if len(raws) > 0 else None
|
||||
exif: str = get_exif(files, raw)
|
||||
readmes: list[str] = [file for file in files if file == "readme.md"]
|
||||
readme: str = readmes[0] if len(readmes) > 0 else None
|
||||
bar.next()
|
||||
|
||||
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)
|
||||
|
||||
general_path: str = os.path.join(output_path, "general")
|
||||
if not os.path.exists(general_path):
|
||||
#os.makedirs(general_path)
|
||||
shutil.copytree("src/templates/", general_path)
|
||||
|
||||
if (__name__ == "__main__"):
|
||||
if __name__ == "__main__":
|
||||
main()
|
Binary file not shown.
Before Width: | Height: | Size: 13 KiB |
@ -1,84 +0,0 @@
|
||||
body {
|
||||
background-color: #002b36;
|
||||
display: flex;
|
||||
justify-content: center; /* Centre l'image */
|
||||
align-items: center; /* Aligne verticalement */
|
||||
position: relative;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#small {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
}
|
||||
|
||||
#download {
|
||||
background-color: lightgray;
|
||||
position: absolute;
|
||||
margin: 10px;
|
||||
right: 200px;
|
||||
top: 200px;
|
||||
}
|
||||
|
||||
#download * {
|
||||
background-color: aqua;
|
||||
display: flex;
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
#download * p {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
#download img {
|
||||
height: 50px;
|
||||
width: 50px;
|
||||
}
|
||||
|
||||
pre {
|
||||
background: #f4f4f4;
|
||||
padding: 10px;
|
||||
border-radius: 5px;
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: "Courier New", monospace;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.dropdown-btn {
|
||||
padding: 10px 20px;
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
border-radius: 5px;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
/* Cacher le checkbox (pour qu'il ne soit pas visible) */
|
||||
.dropdown-checkbox {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Style du texte qui apparaît lorsque la case est cochée */
|
||||
.dropdown-text {
|
||||
display: none;
|
||||
margin-top: 10px;
|
||||
padding: 10px;
|
||||
background-color: #f1f1f1;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
/* Quand la case est cochée, afficher le texte */
|
||||
.dropdown-checkbox:checked + .dropdown-btn + .dropdown-text {
|
||||
display: flex;
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="../general/page.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<img id="small" src="{{ small }}">\
|
||||
{% if large or raw %}
|
||||
<div id="download">
|
||||
{% if large %}
|
||||
<a id="large" href="{{ large }}">
|
||||
<img src="../general/icons/file.png">
|
||||
<p>large</p>
|
||||
</a>
|
||||
{% endif %}
|
||||
{% if raw %}
|
||||
<a id="raw" href="{{ raw }}">
|
||||
<img src="../general/icons/file.png">
|
||||
<p>raw</p>
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if desc %}
|
||||
<div id="description">
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if exif %}
|
||||
<div id="exif">
|
||||
<!-- Checkbox cachée qui contrôle l'affichage du texte -->
|
||||
<input type="checkbox" id="dropdown" class="dropdown-checkbox">
|
||||
|
||||
<!-- Bouton -->
|
||||
<label for="dropdown" class="dropdown-btn">Display exif data</label>
|
||||
<div class="dropdown-text">
|
||||
<pre>
|
||||
<code>
|
||||
{{exif}}
|
||||
</code>
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
20
src/templates/page.jinja
Normal file
20
src/templates/page.jinja
Normal file
@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="../general/page.css">
|
||||
</head>
|
||||
<body>
|
||||
{% if readme %}
|
||||
{{readme}}
|
||||
{% endif %}
|
||||
{% for image in images %}
|
||||
<img src="{{ image }}">
|
||||
{% endfor %}
|
||||
{% if exif %}
|
||||
<a href="{{ exif }}">Exif</a>
|
||||
{% endif %}
|
||||
{% if raw %}
|
||||
<a href="{{ raw }}">RAW</a>
|
||||
{% endif %}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user