python
import flet as ft
from PIL import Image
class CroppingContainer(ft.UserControl):
def __init__(self, image_path: str, on_rect_change=None):
super().__init__()
self.image_path = image_path
self.on_rect_change = on_rect_change
self._rect = None # [x1, y1, x2, y2] in container coords
self._image_size = None # (width, height) in pixels
self._container_size = None # (width, height) in pixels
self._scale = None # scale factor image->container
self._offset = None # (offset_x, offset_y) image inside container (for centering)
self._dragging = False
self._drag_start = None
# Pillowで画像サイズ取得
with Image.open(self.image_path) as img:
self._image_size = img.size # (width, height)
def build(self):
self.image_control = ft.Image(
src=self.image_path,
fit=ft.ImageFit.CONTAIN,
expand=True,
)
self.canvas = ft.Canvas(
expand=True,
on_pointer_down=self._on_pointer_down,
on_pointer_move=self._on_pointer_move,
on_pointer_up=self._on_pointer_up,
)
# Stackで画像とキャンバス重ねる
self.stack = ft.Stack(
expand=True,
controls=[self.image_control, self.canvas]
)
return self.stack
def _update_scale_and_offset(self):
# コンテナサイズ取得
w = self.stack.width
h = self.stack.height
if w is None or h is None:
return False
self._container_size = (w, h)
iw, ih = self._image_size
cw, ch = w, h
# ImageFit.CONTAINのスケール計算
scale_w = cw / iw
scale_h = ch / ih
scale = min(scale_w, scale_h)
self._scale = scale
# 画像がコンテナ内で中央に配置されるためのオフセット
offset_x = (cw - iw * scale) / 2
offset_y = (ch - ih * scale) / 2
self._offset = (offset_x, offset_y)
return True
def _on_pointer_down(self, e: ft.PointerEvent):
if not self._update_scale_and_offset():
return
x, y = e.local_x, e.local_y
ox, oy = self._offset
iw, ih = self._image_size
# 画像内か判定
if not (ox <= x <= ox + iw * self._scale and oy <= y <= oy + ih * self._scale):
return
self._dragging = True
self._drag_start = (x, y)
self._rect = [x, y, x, y]
self._redraw()
def _on_pointer_move(self, e: ft.PointerEvent):
if not self._dragging:
return
x, y = e.local_x, e.local_y
x0, y0 = self._drag_start
# 矩形の左上右下を計算(ドラッグ方向に対応)
x1, x2 = sorted([x0, x])
y1, y2 = sorted([y0, y])
# 画像内に制限
ox, oy = self._offset
iw, ih = self._image_size
x1 = max(ox, min(x1, ox + iw * self._scale))
x2 = max(ox, min(x2, ox + iw * self._scale))
y1 = max(oy, min(y1, oy + ih * self._scale))
y2 = max(oy, min(y2, oy + ih * self._scale))
self._rect = [x1, y1, x2, y2]
self._redraw()
self._notify_rect_change()
def _on_pointer_up(self, e: ft.PointerEvent):
if self._dragging:
self._dragging = False
self._notify_rect_change()
def _redraw(self):
self.canvas.controls.clear()
if self._rect:
x1, y1, x2, y2 = self._rect
rect = ft.Rect(
left=x1,
top=y1,
width=x2 - x1,
height=y2 - y1,
stroke_color=ft.colors.RED,
stroke_width=2,
fill_color=ft.colors.TRANSPARENT,
)
self.canvas.controls.append(rect)
self.canvas.update()
def _notify_rect_change(self):
if self.on_rect_change:
self.on_rect_change(self.rect_area)
@property
def rect_area(self):
"""
画像の実際のピクセル座標での矩形領域を返す。
[x1, x2, y1, y2]の形式。
Noneの場合はNoneを返す。
"""
if not self._rect or not self._scale or not self._offset:
return None
x1, y1, x2, y2 = self._rect
ox, oy = self._offset
scale = self._scale
# コンテナ座標 -> 画像座標
ix1 = int((x1 - ox) / scale)
ix2 = int((x2 - ox) / scale)
iy1 = int((y1 - oy) / scale)
iy2 = int((y2 - oy) / scale)
# 画像サイズ内に制限
iw, ih = self._image_size
ix1 = max(0, min(ix1, iw))
ix2 = max(0, min(ix2, iw))
iy1 = max(0, min(iy1, ih))
iy2 = max(0, min(iy2, ih))
return [ix1, ix2, iy1, iy2]