以下は、CroppingContainer クラスを拡張して、画像をパス経由で渡すと、コンテナいっぱいに画像を表示し、その画像上に矩形を表示する機能を実装したものです。
# src/tests/test_manual_crop_v2.py
from logging import getLogger
from logging.config import dictConfig
from pathlib import Path
from typing import Optional, Union, Callable
import flet as ft
from PIL import Image
from configs.logging_config import LOGGING_CONFIG
from configs.path_config import EXAMPLE_IMAGE
logger_local = getLogger(__name__)
class CroppingContainer:
def __init__(self, src: Union[Path, str] = "", on_rect_changed: Optional[Callable] = None):
self.__container = ft.Container()
self.__image = ft.Image(src=src, width="100%", height="100%", fit="cover")
self.__rect = ft.Container(
width=0,
height=0,
bgcolor="rgba(255, 255, 255, 0.5)",
border_radius=0,
border_width=1,
border_color="black",
)
self.__container.content = ft.Stack(
controls=[self.__image, self.__rect], expand=True
)
self.__on_rect_changed = on_rect_changed
self.__image_path = src
self.__rect_area = [0, 0, 0, 0]
self.__container.on_mouse_move = self.__on_mouse_move
self.__container.on_mouse_down = self.__on_mouse_down
self.__container.on_mouse_up = self.__on_mouse_up
def set_image(self, image: Path | str):
self.__image.src = image
self.__image_path = image
self.__rect.width = 0
self.__rect.height = 0
self.__rect.left = 0
self.__rect.top = 0
self.__rect_area = [0, 0, 0, 0]
@property
def container(self) -> ft.Container:
return self.__container
@property
def rect(self) -> tuple[int, int, int, int]:
return self.__rect_area
@property
def rect_area(self) -> list[int, int, int, int]:
return self.__rect_area
def __on_mouse_down(self, e: ft.ControlEvent):
self.__start_x = e.page_x
self.__start_y = e.page_y
self.__rect.left = e.page_x - self.__container.left
self.__rect.top = e.page_y - self.__container.top
self.__rect.width = 0
self.__rect.height = 0
self.__container.update()
def __on_mouse_move(self, e: ft.ControlEvent):
if e.mouse_button == ft.MouseButton.PRIMARY:
self.__rect.width = abs(e.page_x - self.__start_x)
self.__rect.height = abs(e.page_y - self.__start_y)
if e.page_x < self.__start_x:
self.__rect.left = e.page_x - self.__container.left
if e.page_y < self.__start_y:
self.__rect.top = e.page_y - self.__container.top
self.__container.update()
self.__update_rect_area()
def __on_mouse_up(self, e: ft.ControlEvent):
self.__update_rect_area()
if self.__on_rect_changed:
self.__on_rect_changed(self.__rect_area)
def __update_rect_area(self):
image = Image.open(self.__image_path)
image_width, image_height = image.size
container_width = self.__container.width
container_height = self.__container.height
x1 = int((self.__rect.left / container_width) * image_width)
y1 = int((self.__rect.top / container_height) * image_height)
x2 = int(((self.__rect.left + self.__rect.width) / container_width) * image_width)
y2 = int(((self.__rect.top + self.__rect.height) / container_height) * image_height)
self.__rect_area = [x1, x2, y1, y2]
def main(page: ft.Page):
container = CroppingContainer(EXAMPLE_IMAGE).container
container.expand = True
page.add(container)
if __name__ == "__main__":
dictConfig(LOGGING_CONFIG)
ft.app(main)