imgconv for lod
This commit is contained in:
parent
3e6c8aa179
commit
038b5a6bf1
88
gen.py
88
gen.py
@ -1,50 +1,67 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import argparse, json, sys, markdown, shutil, json, re, pathlib, shutil
|
||||
import utils.imgconv
|
||||
|
||||
input_dir = None
|
||||
output_dir = None
|
||||
style = None
|
||||
(W, H, FMT) = (None, None, None)
|
||||
codebase = pathlib.Path(__file__).parent.absolute()
|
||||
|
||||
def translate_index_header(txt, dirname):
|
||||
href = None
|
||||
sections = txt.count('- - -');
|
||||
sections = txt.count('- - -')
|
||||
if sections == 0:
|
||||
# not following the normal template -> direct html
|
||||
txt = txt.replace('data/', dirname + '/'); #<--- replace reference to data/
|
||||
txt = txt.replace('data/', dirname + '/') #<--- replace reference to data/
|
||||
return txt
|
||||
elif sections == 1:
|
||||
[text, link] = txt.split('- - -');
|
||||
elif sections == 2:
|
||||
[text, link, href] = txt.split('- - -');
|
||||
[text, link] = txt.split('- - -')
|
||||
|
||||
text_md = markdown.markdown(text);
|
||||
link_md = markdown.markdown(link);
|
||||
text_md = markdown.markdown(text)
|
||||
link_info = json.loads(link)
|
||||
|
||||
# revise this - perhaps with a better
|
||||
link_md = link_md.replace('data/', dirname + '/');
|
||||
dirpath = pathlib.Path(dirname)
|
||||
img_name = pathlib.Path(link_info['img']).name
|
||||
img_path = dirpath / img_name
|
||||
img_resize_path = dirpath / "img-resize" / img_name
|
||||
|
||||
picture_el = emit_picture(str(img_path), str(img_resize_path))
|
||||
|
||||
table = codebase / "templates/<table>"
|
||||
with table.open(encoding="utf-8") as fp:
|
||||
out = fp.read()
|
||||
|
||||
out = out.replace('[[text_md]]', text_md);
|
||||
out = out.replace('[[link_md]]', link_md);
|
||||
if href:
|
||||
href = href.replace('data/', dirname + '/');
|
||||
out = "<a " + href + ">" + out + "</a>";
|
||||
out = out.replace('[[link_md]]', picture_el);
|
||||
if link_info['href']:
|
||||
if link_info['href'] == "data":
|
||||
href = dirname + "/"
|
||||
else:
|
||||
href = link_info['href']
|
||||
out = "<a href='" + href + "'>" + out + "</a>";
|
||||
return out
|
||||
|
||||
def escape_date(dirname):
|
||||
return re.sub('^20\d{2}?.', '', dirname)
|
||||
|
||||
def emit_picture(src_full, src_resize):
|
||||
return f'''<picture>
|
||||
<source media="(max-width: 799px)" srcset="{src_resize}" />
|
||||
<source media="(min-width: 800px)" srcset="{src_full}" />
|
||||
<img src="{src_full}" />
|
||||
</picture>'''
|
||||
|
||||
|
||||
def emit_img(file: pathlib.Path, data_dir: pathlib.Path):
|
||||
|
||||
## this is where LOD can be applied
|
||||
resize_dir = data_dir / "img-resize"
|
||||
resize_dir.mkdir(parents=True, exist_ok=True)
|
||||
filepath = data_dir / file
|
||||
resize_file = utils.imgconv.convert(filepath, (W, H, FMT), resize_dir)
|
||||
|
||||
return f'<a href="{file}"><img src="{file}" /></a>'
|
||||
return f'<a href="{file}">' + emit_picture(str(file), "img-resize/" + resize_file.name) + '</a>'
|
||||
|
||||
def emit_video_mp4(file: pathlib.Path, data_dir: pathlib.Path):
|
||||
return f'<video controls><source src="{file}" type="video/mp4"</video>'
|
||||
@ -93,6 +110,7 @@ def default(file, data_dir):
|
||||
content_map = {
|
||||
'.png': emit_img,
|
||||
'.jpg': emit_img,
|
||||
'.jpeg': emit_img,
|
||||
'.m4v': emit_video_mp4,
|
||||
'.mov': emit_video_mp4,
|
||||
'.mp4': emit_video_mp4,
|
||||
@ -134,7 +152,7 @@ def index_content(dir_name: str, data_dir: pathlib.Path, index_txt: pathlib.Path
|
||||
for i in content_index:
|
||||
f = data_dir / i
|
||||
if f.is_file():
|
||||
files.append(i);
|
||||
files.append(data_dir / i);
|
||||
else:
|
||||
files += [pathlib.Path(a.name) for a in list(data_dir.glob(i))]
|
||||
|
||||
@ -148,6 +166,9 @@ def index_content(dir_name: str, data_dir: pathlib.Path, index_txt: pathlib.Path
|
||||
if element:
|
||||
content += "<li>" + element + "</li>" + "\n";
|
||||
|
||||
|
||||
# check if there is an html file existing already. if so use that one.
|
||||
|
||||
html = template.replace('[[content]]', content).replace('[[dir]]', str(dir_name));
|
||||
|
||||
try:
|
||||
@ -180,6 +201,7 @@ if __name__ == '__main__':
|
||||
p.add_argument('--output', "-o", help='output directory');
|
||||
p.add_argument('--style', "-y", default="+++", help='css style, ico, etc.');
|
||||
p.add_argument("--settings", "-s", default=".static/settings.json", help="Settings file (default: .static/settings.json")
|
||||
p.add_argument('--format', '-f', default="1000x1000", help='img output formats (ex. 1000x)');
|
||||
p.add_argument('--clean', help='cleans the output directory');
|
||||
|
||||
args = p.parse_args();
|
||||
@ -201,15 +223,20 @@ if __name__ == '__main__':
|
||||
if inputdir is not None:
|
||||
input_dir = inputdir;
|
||||
else:
|
||||
print(f"Input directory {args.input} is not a directory... Aborting.")
|
||||
sys.exit(1)
|
||||
sys.exit(f"Input directory {args.input} is not a directory... Aborting.")
|
||||
|
||||
if args.output:
|
||||
outputdir = check_dir_exists(args.output, create=True)
|
||||
if outputdir is not None:
|
||||
output_dir = outputdir;
|
||||
else:
|
||||
print(f"Output directory {args.output} is not a directory... Aborting.")
|
||||
sys.exit(1)
|
||||
sys.exit(f"Output directory {args.output} is not a directory... Aborting.")
|
||||
|
||||
if args.format:
|
||||
(W, H, FMT) = utils.imgconv.parse_format(args.format)
|
||||
if not FMT:
|
||||
sys.exit(f"Format {str(args.format)} is not valid. Aborting.")
|
||||
|
||||
|
||||
if style is not None:
|
||||
style_dir = codebase / f"styles/{style}"
|
||||
@ -217,8 +244,7 @@ if __name__ == '__main__':
|
||||
style = args.style
|
||||
|
||||
if input_dir is None:
|
||||
print(f"Error with input directory... Aborting.")
|
||||
sys.exit(1)
|
||||
sys.exit(f"Error with input directory... Aborting.")
|
||||
|
||||
if output_dir is None:
|
||||
print(f"No output directory.")
|
||||
@ -236,7 +262,6 @@ if __name__ == '__main__':
|
||||
except:
|
||||
print(f"error erasing {output_dir}. Continuing.")
|
||||
|
||||
|
||||
print('1/3 - Configuring');
|
||||
|
||||
# main index template
|
||||
@ -273,11 +298,9 @@ if __name__ == '__main__':
|
||||
section_indx_txt = datadir / "index.txt"
|
||||
section_desc_txt = datadir / "desc.txt"
|
||||
|
||||
out_index = None
|
||||
if section_indx_txt.is_file() or section_desc_txt.is_file():
|
||||
out_index = index_content(dirname, d, section_indx_txt, section_desc_txt, section_indx_template_str);
|
||||
|
||||
if out_index is None:
|
||||
continue
|
||||
out_index = index_content(dirname, datadir, section_indx_txt, section_desc_txt, section_indx_template_str)
|
||||
|
||||
if output_dir is not None:
|
||||
out_datadir = output_dir / dirname
|
||||
@ -298,11 +321,12 @@ if __name__ == '__main__':
|
||||
continue
|
||||
|
||||
# copy index after (since index.html might be lurking in content dirs)
|
||||
try:
|
||||
shutil.copy2(out_index, out_datadir)
|
||||
except:
|
||||
print(f"error copying {out_index} to {out_datadir}. Continuing.")
|
||||
continue
|
||||
if out_index:
|
||||
try:
|
||||
shutil.copy2(out_index, out_datadir)
|
||||
except:
|
||||
print(f"error copying {out_index} to {out_datadir}. Continuing.")
|
||||
continue
|
||||
|
||||
print('3/3 - Generating ouput');
|
||||
|
||||
|
||||
@ -21,7 +21,8 @@ case $1 in
|
||||
;;
|
||||
run)
|
||||
echo "running"
|
||||
# source venv/bin/activate
|
||||
source venv/bin/activate
|
||||
export MAGICK_HOME=/opt/homebrew/Cellar/imagemagick/7.1.1-13
|
||||
# exec python $2 "${@:3}"
|
||||
esac
|
||||
|
||||
|
||||
@ -20,57 +20,19 @@
|
||||
<pre>
|
||||
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||
|
||||
mQINBFwRHKQBEACm4RTJEAvqrczYPNDtu08mB7PQ6mouSgWNLxPIY0QWNEhZJKLE
|
||||
bPj8eE5sG6gSaNTePC0RVBZDhNC9YXHJiMPaa2Av4LBK+xQSi4oMxB3qaWzRpNm1
|
||||
1iL7SKNwBcIVdBYOyhWl+AyOlMUIdOsDpC8Zo3qgTDaW45tn+DjvqY5yA+voNwln
|
||||
tk1Fwftl0tPeP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CK-----
|
||||
</pre>
|
||||
mDMEZLoz/RYJKwYBBAHaRw8BAQdAsuOzLRb5SR4kJKpiFcP3XhPYyrcm8lqJQCzq
|
||||
Kgq0cau0RURhdmlkIEdhdXRoaWVyICg3ZSBzw6lyaWUsIGRlcyBtb3RzIMOpc290
|
||||
w6lyaXF1ZXMpIDxkQGdhdXRoaWllci5pbmZvPoiZBBMWCgBBFiEEdUjrpohoTnvh
|
||||
RwQPqCZfthzndOcFAmS6M/0CGwMFCQWjmoAFCwkIBwICIgIGFQoJCAsCBBYCAwEC
|
||||
HgcCF4AACgkQqCZfthzndOex0gEAqZKLSpIX57S0EzVrpazSYbMtRppsnndvWACD
|
||||
w0TdFd4A/jKxS2FCCbUeMNwSz8iRjGRYiTZtJytQ6W3SKEQlyW8CuDgEZLoz/RIK
|
||||
KwYBBAGXVQEFAQEHQFI6QyMPtYSOXCbLaPxaR8jpzjg1tqJZA4vvpOVOfdZsAwEI
|
||||
B4h+BBgWCgAmFiEEdUjrpohoTnvhRwQPqCZfthzndOcFAmS6M/0CGwwFCQWjmoAA
|
||||
CgkQqCZfthzndOdPrAD+NgSQRfndE2d9IiFAQDP7UNbqh0e731/ktyg2mwukudUB
|
||||
AM+9BMj8ZAV+2cDSlf7Muk+SCnYd/uCy1ulJ16vKYJAC
|
||||
=QFke
|
||||
|
||||
-----END PGP PUBLIC KEY BLOCK-----</pre>
|
||||
<br>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@ -31,12 +31,21 @@ table td h1 {
|
||||
margin-bottom: 0.2em;
|
||||
}
|
||||
|
||||
h1 a:hover, h1 a:active {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
|
||||
table td p {
|
||||
bborder: 1px red solid;
|
||||
margin-top: 0.1em;
|
||||
margin-bottom: 0.4em;
|
||||
}
|
||||
|
||||
a:hover p, a:active p {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
|
||||
table tr {
|
||||
text-align: center;
|
||||
|
||||
82
utils/imgconv.py
Normal file
82
utils/imgconv.py
Normal file
@ -0,0 +1,82 @@
|
||||
import argparse, pathlib, subprocess, sys
|
||||
from wand.image import Image
|
||||
|
||||
supported_glob = "[jpJP][npNP][egEG]"
|
||||
|
||||
def sanity_check_system():
|
||||
r = subprocess.call("magick -version", shell=True) == 0
|
||||
if not r:
|
||||
sys.exit("ImageMagick not installed. Aborting...")
|
||||
|
||||
def parse_format(format_str):
|
||||
fmt = format_str.lower()
|
||||
if 'x' in fmt:
|
||||
w_h = fmt.split('x')
|
||||
w = int(w_h[0]) if w_h[0] != '' else 0
|
||||
h = int(w_h[1]) if w_h[1] != '' else 0
|
||||
return(w, h, fmt)
|
||||
return (None, None, None)
|
||||
|
||||
def convert(filepath : pathlib.Path, fmt : tuple, output_dir: pathlib.Path):
|
||||
(w, h, f) = fmt
|
||||
|
||||
# img exists?
|
||||
output = output_dir / filepath.name
|
||||
if output.exists() and output.is_file():
|
||||
with Image(filename=str(output)) as img:
|
||||
if w > 0 and img.width == w or h > 0 and img.height == h:
|
||||
return output
|
||||
|
||||
with Image(filename=str(filepath)) as img:
|
||||
print(f". converting {filepath}")
|
||||
print(f" > {img.size}")
|
||||
img.resolution = (72, 72)
|
||||
if w > 0 and img.width > w or h > 0 and img.height > h:
|
||||
img.transform(resize=f)
|
||||
print(f" > {img.size}")
|
||||
img.save(filename=str(output))
|
||||
return output
|
||||
|
||||
def convert_all(input_dir: pathlib.Path, fmt : tuple, output_dir: pathlib.Path):
|
||||
img_files = input_dir.glob("*." + supported_glob + "*")
|
||||
for i in img_files:
|
||||
convert(i, fmt, output_dir)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
p = argparse.ArgumentParser(description='Converts images files to other formats (using wand/ImageMagick)');
|
||||
p.add_argument('-i', '--input', help='input directory');
|
||||
p.add_argument('-o', '--output', help='output directory');
|
||||
p.add_argument('-f', '--format', help='output formats (ex. 1000x)');
|
||||
|
||||
sanity_check_system()
|
||||
|
||||
args = p.parse_args()
|
||||
|
||||
if not args.format:
|
||||
sys.exit("No formats specified. Aborting.")
|
||||
|
||||
input_dir = pathlib.Path(".")
|
||||
if args.input:
|
||||
input_dir = pathlib.Path(args.input)
|
||||
|
||||
if not input_dir.exists() or not input_dir.is_dir():
|
||||
sys.exit(f"Input {str(input_dir)} is not a directory. Aborting.")
|
||||
|
||||
output_dir = input_dir / "img-resize"
|
||||
if args.output:
|
||||
output_dir = pathlib.Path(args.output)
|
||||
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
(w, h, fmt) = parse_format(args.format)
|
||||
if not fmt:
|
||||
sys.exit(f"Format {str(args.format)} is not valid. Aborting.")
|
||||
|
||||
img_files = input_dir.glob("*." + supported_glob + "*")
|
||||
for i in img_files:
|
||||
convert(i, (w, h, fmt), output_dir)
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user