updates preview and pauses
This commit is contained in:
65
utils.py
65
utils.py
@@ -167,17 +167,7 @@ def fetch_positions_for_selected_day(settings, device_mapping, device_name, star
|
||||
return [], error
|
||||
return positions, None
|
||||
|
||||
def html_to_image(html_path, img_path, width=800, height=600, delay=2, driver_path='/usr/bin/chromedriver'):
|
||||
"""
|
||||
Convert an HTML file to an image using Selenium and Pillow.
|
||||
Args:
|
||||
html_path (str): Path to the HTML file.
|
||||
img_path (str): Path to save the output image (PNG).
|
||||
width (int): Width of the browser window.
|
||||
height (int): Height of the browser window.
|
||||
delay (int): Seconds to wait for the page to render.
|
||||
driver_path (str): Path to chromedriver binary.
|
||||
"""
|
||||
def html_to_image(html_path, img_path, width=1280, height=720, delay=2, driver_path='/usr/bin/chromedriver'):
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.service import Service
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
@@ -185,9 +175,11 @@ def html_to_image(html_path, img_path, width=800, height=600, delay=2, driver_pa
|
||||
import time
|
||||
import os
|
||||
|
||||
selenium_height = int(height * 1.2) # 10% taller for compensation
|
||||
|
||||
chrome_options = Options()
|
||||
chrome_options.add_argument("--headless")
|
||||
chrome_options.add_argument(f"--window-size={width},{height}")
|
||||
chrome_options.add_argument(f"--window-size={width},{selenium_height}")
|
||||
chrome_options.add_argument("--no-sandbox")
|
||||
chrome_options.add_argument("--disable-dev-shm-usage")
|
||||
|
||||
@@ -195,17 +187,18 @@ def html_to_image(html_path, img_path, width=800, height=600, delay=2, driver_pa
|
||||
driver = webdriver.Chrome(service=service, options=chrome_options)
|
||||
|
||||
try:
|
||||
driver.set_window_size(width, selenium_height)
|
||||
driver.get("file://" + os.path.abspath(html_path))
|
||||
time.sleep(delay) # Wait for the page to render
|
||||
time.sleep(delay)
|
||||
tmp_img = img_path + ".tmp.png"
|
||||
driver.save_screenshot(tmp_img)
|
||||
driver.quit()
|
||||
|
||||
img = Image.open(tmp_img)
|
||||
img = img.crop((0, 0, width, height))
|
||||
img = img.crop((0, 0, width, height)) # Crop to original map size
|
||||
img.save(img_path)
|
||||
os.remove(tmp_img)
|
||||
print(f"Image saved to: {img_path}")
|
||||
print(f"Image saved to: {img_path} ({width}x{height})")
|
||||
except Exception as e:
|
||||
print(f"Error converting HTML to image: {e}")
|
||||
driver.quit()
|
||||
@@ -247,15 +240,53 @@ def process_preview_util(
|
||||
return
|
||||
|
||||
coords = [(pos['latitude'], pos['longitude']) for pos in positions]
|
||||
width, height = 1280, 720 # 16:9 HD
|
||||
|
||||
m = folium.Map(
|
||||
location=coords[0],
|
||||
width=width,
|
||||
height=height
|
||||
height=height,
|
||||
control_scale=True
|
||||
)
|
||||
folium.PolyLine(coords, color="blue", weight=4.5, opacity=1).add_to(m)
|
||||
folium.Marker(coords[0], tooltip="Start", icon=folium.Icon(color="green")).add_to(m)
|
||||
folium.Marker(coords[-1], tooltip="End", icon=folium.Icon(color="red")).add_to(m)
|
||||
m.fit_bounds(coords)
|
||||
|
||||
# --- Add pause markers if pauses.json exists ---
|
||||
pauses_path = os.path.join(project_folder, "pauses.json")
|
||||
if os.path.exists(pauses_path):
|
||||
with open(pauses_path, "r") as pf:
|
||||
pauses = json.load(pf)
|
||||
for pause in pauses:
|
||||
lat = pause["location"]["latitude"]
|
||||
lon = pause["location"]["longitude"]
|
||||
duration = pause["duration_seconds"]
|
||||
start = pause["start_time"]
|
||||
end = pause["end_time"]
|
||||
folium.Marker(
|
||||
[lat, lon],
|
||||
tooltip=f"Pause: {duration//60} min {duration%60} sec",
|
||||
popup=f"Pause from {start} to {end} ({duration//60} min {duration%60} sec)",
|
||||
icon=folium.Icon(color="orange", icon="pause", prefix="fa")
|
||||
).add_to(m)
|
||||
|
||||
m.fit_bounds(coords, padding=(80, 80))
|
||||
m.get_root().html.add_child(folium.Element(f"""
|
||||
<style>
|
||||
html, body {{
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
}}
|
||||
#{m.get_name()} {{
|
||||
position: absolute;
|
||||
top: 0; bottom: 0; left: 0; right: 0;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
}}
|
||||
</style>
|
||||
"""))
|
||||
m.save(html_path)
|
||||
|
||||
html_to_image(html_path, img_path, width=width, height=height)
|
||||
|
||||
Reference in New Issue
Block a user