updated to view photo
This commit is contained in:
41
main.py
41
main.py
@@ -21,6 +21,11 @@ from kivy.clock import mainthread
|
||||
from kivy.uix.image import Image
|
||||
from kivy.uix.behaviors import ButtonBehavior
|
||||
from kivy.uix.progressbar import ProgressBar
|
||||
import webbrowser
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
from PIL import Image
|
||||
import time
|
||||
|
||||
kivy.require("2.0.0")
|
||||
from kivy.core.window import Window
|
||||
@@ -471,6 +476,7 @@ class RegisterScreen(Screen):
|
||||
class CreateAnimationScreen(Screen):
|
||||
project_name = StringProperty("")
|
||||
preview_html_path = StringProperty("") # Path to the HTML file for preview
|
||||
preview_image_path = StringProperty("") # Add this line
|
||||
|
||||
def on_pre_enter(self):
|
||||
# Update the route entries label with the actual number of entries
|
||||
@@ -626,11 +632,11 @@ class CreateAnimationScreen(Screen):
|
||||
def process_preview(dt):
|
||||
try:
|
||||
import folium
|
||||
import webbrowser
|
||||
|
||||
project_folder = os.path.join(RESOURCES_FOLDER, "projects", self.project_name)
|
||||
positions_path = os.path.join(project_folder, "positions.json")
|
||||
html_path = os.path.join(project_folder, "preview.html")
|
||||
img_path = os.path.join(project_folder, "preview.png")
|
||||
|
||||
if not os.path.exists(positions_path):
|
||||
label.text = "positions.json not found!"
|
||||
@@ -645,18 +651,18 @@ class CreateAnimationScreen(Screen):
|
||||
progress.value = 100
|
||||
return
|
||||
|
||||
# Get coordinates
|
||||
coords = [(pos['latitude'], pos['longitude']) for pos in positions]
|
||||
|
||||
# Center map on first point
|
||||
m = folium.Map(location=coords[0], zoom_start=14)
|
||||
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.save(html_path)
|
||||
|
||||
# Set the path for the WebView
|
||||
self.preview_html_path = "file://" + os.path.abspath(html_path)
|
||||
# Convert HTML to image
|
||||
save_folium_map_as_image(html_path, img_path)
|
||||
|
||||
# Set the image path for Kivy Image widget
|
||||
self.preview_image_path = img_path
|
||||
label.text = "Preview ready!"
|
||||
progress.value = 100
|
||||
|
||||
@@ -672,6 +678,29 @@ class CreateAnimationScreen(Screen):
|
||||
Clock.schedule_once(close_popup, 2)
|
||||
|
||||
Clock.schedule_once(process_preview, 0.5)
|
||||
|
||||
def save_folium_map_as_image(html_path, img_path, width=800, height=600, delay=2):
|
||||
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")
|
||||
driver = webdriver.Chrome(options=chrome_options)
|
||||
|
||||
try:
|
||||
driver.get("file://" + os.path.abspath(html_path))
|
||||
time.sleep(delay) # Wait for map to render
|
||||
screenshot_path = img_path + ".tmp.png"
|
||||
driver.save_screenshot(screenshot_path)
|
||||
driver.quit()
|
||||
|
||||
img = Image.open(screenshot_path)
|
||||
img = img.crop((0, 0, width, height))
|
||||
img.save(img_path)
|
||||
os.remove(screenshot_path)
|
||||
except Exception as e:
|
||||
print(f"Error saving folium map as image: {e}")
|
||||
driver.quit()
|
||||
|
||||
|
||||
class HomeScreen(Screen):
|
||||
|
||||
Reference in New Issue
Block a user