saved preview picture
This commit is contained in:
43
utils.py
43
utils.py
@@ -166,3 +166,46 @@ def fetch_positions_for_selected_day(settings, device_mapping, device_name, star
|
||||
if error:
|
||||
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.
|
||||
"""
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.service import Service
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
from PIL import Image
|
||||
import time
|
||||
import os
|
||||
|
||||
chrome_options = Options()
|
||||
chrome_options.add_argument("--headless")
|
||||
chrome_options.add_argument(f"--window-size={width},{height}")
|
||||
chrome_options.add_argument("--no-sandbox")
|
||||
chrome_options.add_argument("--disable-dev-shm-usage")
|
||||
|
||||
service = Service(driver_path)
|
||||
driver = webdriver.Chrome(service=service, options=chrome_options)
|
||||
|
||||
try:
|
||||
driver.get("file://" + os.path.abspath(html_path))
|
||||
time.sleep(delay) # Wait for the page to render
|
||||
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.save(img_path)
|
||||
os.remove(tmp_img)
|
||||
print(f"Image saved to: {img_path}")
|
||||
except Exception as e:
|
||||
print(f"Error converting HTML to image: {e}")
|
||||
driver.quit()
|
||||
Reference in New Issue
Block a user