Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def create_new_vehicle_dir(new_vehicle_dir: str) -> str:
@staticmethod
def valid_directory_name(dir_name: str) -> bool:
"""
Check if a name contains only alphanumeric characters, underscores, hyphens and the OS directory separator.
Check if a name contains only alphanumeric characters, underscores, hyphens, dots and the OS directory separator.

This function is designed to ensure that the directory name does not contain characters that are
invalid for directory names in many operating systems. It does not guarantee that the name
Expand All @@ -141,8 +141,8 @@ def valid_directory_name(dir_name: str) -> bool:
bool: True if the directory name matches the allowed pattern, False otherwise.

"""
# Include os.sep in the pattern
pattern = r"^[\w" + re_escape(os_sep) + "-]+$"
# Include os.sep and dot in the pattern
pattern = r"^[\w" + re_escape(os_sep) + ".-]+$"
return re_match(pattern, dir_name) is not None

@staticmethod
Expand Down
62 changes: 41 additions & 21 deletions ardupilot_methodic_configurator/frontend_tkinter_base_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# https://wiki.tcl-lang.org/page/Changing+Widget+Colors

import io
import os
import tkinter as tk
import tkinter.font as tkfont
Expand All @@ -30,7 +31,7 @@
from tkinter import ttk
from typing import Optional, Union

from PIL import Image, ImageTk
from PIL import Image

from ardupilot_methodic_configurator import _
from ardupilot_methodic_configurator.backend_filesystem import LocalFilesystem
Expand Down Expand Up @@ -262,7 +263,7 @@ def center_window(window: Union[tk.Toplevel, tk.Tk], parent: Union[tk.Toplevel,
y = parent.winfo_y() + (parent_height // 2) - (window_height // 2)
window.geometry(f"+{x}+{y}")

def put_image_in_label(
def put_image_in_label( # pylint: disable=too-many-locals
self,
parent: ttk.Frame,
filepath: str,
Expand Down Expand Up @@ -320,25 +321,44 @@ def put_image_in_label(
raise FileNotFoundError(msg)

# Load and validate the image
image = Image.open(filepath)
if image is None:
msg = _("Failed to load image from %s") % filepath
raise ValueError(msg)

# Calculate new dimensions while preserving aspect ratio
width, height = image.size
if height == 0:
msg = _("Image has zero height")
raise ValueError(msg)

aspect_ratio = width / height
dpi_scaled_height = int(image_height * self.dpi_scaling_factor)

# Resize the image
resized_image = image.resize((int(dpi_scaled_height * aspect_ratio), dpi_scaled_height), Image.Resampling.LANCZOS)

# Convert to PhotoImage for Tkinter
photo = ImageTk.PhotoImage(resized_image)
with Image.open(filepath) as image:
if image is None:
msg = _("Failed to load image from %s") % filepath
raise ValueError(msg)

# Calculate new dimensions while preserving aspect ratio
width, height = image.size
if height == 0:
msg = _("Image has zero height")
raise ValueError(msg)
if width == 0:
msg = _("Image has zero width")
raise ValueError(msg)

aspect_ratio = width / height
dpi_scaled_height = int(image_height * self.dpi_scaling_factor)

# Ensure minimum dimensions to prevent resize errors
dpi_scaled_height = max(dpi_scaled_height, 1)

calculated_width = int(dpi_scaled_height * aspect_ratio)
calculated_width = max(calculated_width, 1)

# Resize the image
resized_image = image.resize((calculated_width, dpi_scaled_height), Image.Resampling.LANCZOS)

# Convert to PhotoImage for Tkinter using a buffer approach
buffer = io.BytesIO()
# Ensure the image is in a format that tk.PhotoImage can handle
if resized_image.mode not in ("RGB", "RGBA"):
resized_image = resized_image.convert("RGB")

# Save as PNG to buffer
resized_image.save(buffer, format="PNG")
buffer.seek(0)

# Create PhotoImage from buffer
photo = tk.PhotoImage(data=buffer.getvalue())

# Create the label with the image
image_label = ttk.Label(parent, image=photo)
Expand Down
Loading
Loading