Skip to content

Instantly share code, notes, and snippets.

@sam2332
Last active July 18, 2024 20:04
Show Gist options
  • Save sam2332/1129b36bba944710aef45466523ab3d5 to your computer and use it in GitHub Desktop.
Save sam2332/1129b36bba944710aef45466523ab3d5 to your computer and use it in GitHub Desktop.
pyscript to generate a history of camera images
#render html file with table of Image, Dates in rows, from the espcam_history folder with filenames lik livingroom-2024-04-23_20-36.jpg
import os
import sys
import datetime
import re
import subprocess
import argparse
import jinja2
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def get_images(folder_path,selection_hours):
images = []
for filename in os.listdir(folder_path):
if filename.endswith(".jpg"):
date_match = re.search(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2})', filename)
if date_match:
date = datetime.datetime.strptime(date_match.group(0), '%Y-%m-%d_%H-%M')
if date > datetime.datetime.now() - datetime.timedelta(hours=selection_hours):
images.append({'filename': filename, 'date': date})
images.sort(key=lambda x: x['date'], reverse=True) # Sort images by date in reverse order
return images
def render_html(images, base_url, output_path):
template_content ="""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="10">
<meta http-equiv="Cache-control" content="no-cache">
<title>Image Table</title>
<style>
body {
background-color: black;
color: gray;
}
table {
width: 100%;
height: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
th, td {
padding: 8px;
text-align: left;
}
.image-container {
position: relative;
width: 100%;
}
img {
width: 100%;
height: auto;
}
.overlay {
position: absolute;
bottom: 10px;
left: 10px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 5px;
font-size: larger;
}
</style>
</head>
<body>
<table>
{% for image in images %}
<tr>
<td>
<div class="image-container">
<img src="{{ base_url }}/{{ image.filename }}" alt="Image">
<div class="overlay">{{ image.date.strftime('%Y-%m-%d %-I:%M %p') }}</div>
</div>
</td>
</tr>
<tr><td></td></tr>
{% endfor %}
</table>
</body>
</html>
"""
template = jinja2.Template(template_content)
html_content = template.render(images=images, base_url=base_url)
with open(output_path, 'w') as f:
f.write(html_content)
@service
@time_trigger
@state_trigger("binary_sensor.entrance_motion == 'on'")
def generate_espcam_history():
folder_path='/config/www/espcam_history'
output_path='/config/www/espcam_history.html'
base_url='/local/espcam_history'
selection_hours=48
images = get_images(folder_path,selection_hours)
logger.info(f'Found {len(images)} images in {folder_path}')
render_html(images, base_url, output_path)
@sam2332
Copy link
Author

sam2332 commented Jul 18, 2024

updated template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment