o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
Untitled conversation
- User
# src/tests/test_manual_crop.py import flet as ft from configs.path_config import EXAMPLE_IMAGE def main(page: ft.Page): rect_left = 50 rect_top = 50 rect_width = 200 rect_height = 150 rect_container = ft.Ref[ft.Container]() # 矩形自体の参照 handle_container = ft.Ref[ft.Container]() # ハンドルの参照(必要なら) # ▲ on_pan_update: ドラッグしたら矩形のサイズを変更 def on_pan_update(e: ft.DragUpdateEvent): nonlocal rect_width, rect_height rect_width += e.delta_x rect_height += e.delta_y # 最小サイズ制限 if rect_width < 20: rect_width = 20 if rect_height < 20: rect_height = 20 rect_container.current.width = rect_width rect_container.current.height = rect_height page.update() # ▼ リサイズハンドル(目に見えてマウスヒットする要素) resize_handle = ft.Container( ref=handle_container, width=16, height=16, bgcolor=ft.Colors.WHITE54, border=ft.border.all(1, ft.Colors.BLACK), alignment=ft.alignment.center, border_radius=8, ink=True, # ❗ これが重要! → ヒット可能な領域になる ) # ▼ GestureDetectorでラップ handle_gesture = ft.GestureDetector( content=resize_handle, mouse_cursor=ft.MouseCursor.RESIZE_UP, drag_interval=1, on_pan_update=on_pan_update, ) # ▼ リサイズ可能矩形 crop_rect = ft.Container( ref=rect_container, left=rect_left, top=rect_top, width=rect_width, height=rect_height, border=ft.border.all(3, ft.Colors.WHITE), bgcolor=None, content=ft.Stack( controls=[ # リサイズハンドルを右下に配置 ft.Container( content=handle_gesture, alignment=ft.alignment.bottom_right, expand=True, # ハンドルの親がrect全体に広がるように ) ] ), ) # Stack: 背景画像 + 矩形 stack = ft.Stack( controls=[ ft.Image( src=EXAMPLE_IMAGE, width=400, height=300, fit=ft.ImageFit.CONTAIN, ), crop_rect, ], width=400, height=300, ) page.add(stack) ft.app(target=main)画像の上に矩形を表示し、その矩形の角を自由に動かすことで、画像の任意の領域を選択できるようにしたい。 現状は左下にハンドルが表示されるが、それ以外はうまくいっていない。 完全に動作するようにして、コード全体を出力して。
- Assistant
こちらが「画像の上に矩形を表示し,各角のハンドルをドラッグして自由にリサイズ・移動できる」フルサンプルコードです。4 つのハンドル(左上・右上・左下・右下)に加え,矩形全体をドラッグして移動する機能も備えています。
# src/tests/test_manual_crop.py import flet as ft from configs.path_config import EXAMPLE_IMAGE def main(page: ft.Page): page.title = "マニュアルクロップデモ" # 初期座標・サイズ rect_left = 50 rect_top = 50 rect_width = 200 rect_height = 150 min_size = 20 # 矩形の最小幅・高さ # 矩形コンテナの参照 rect_ref = ft.Ref[ft.Container]() # ハンドルのサイズ handle_size = 12 # ==== ヘルパー: 矩形に反映&再描画 ==== def update_rect(): c = rect_ref.current c.left = rect_left c.top = rect_top c.width = rect_width c.height = rect_height page.update() # ==== 矩形移動用 ==== def on_pan_move(e: ft.DragUpdateEvent): nonlocal rect_left, rect_top rect_left += e.delta_x rect_top += e.delta_y update_rect() # ==== 各コーナーのリサイズ ==== def on_pan_br(e: ft.DragUpdateEvent): # bottom-right nonlocal rect_width, rect_height rect_width += e.delta_x rect_height += e.delta_y if rect_width < min_size: rect_width = min_size if rect_height < min_size: rect_height = min_size update_rect() def on_pan_bl(e: ft.DragUpdateEvent): # bottom-left nonlocal rect_left, rect_width, rect_height rect_left += e.delta_x rect_width -= e.delta_x rect_height += e.delta_y if rect_width < min_size: # 左端を調整して幅を最小に保つ rect_left -= (min_size - rect_width) rect_width = min_size if rect_height < min_size: rect_height = min_size update_rect() def on_pan_tr(e: ft.DragUpdateEvent): # top-right nonlocal rect_top, rect_width, rect_height rect_width += e.delta_x rect_top += e.delta_y rect_height -= e.delta_y if rect_width < min_size: rect_width = min_size if rect_height < min_size: rect_top -= (min_size - rect_height) rect_height = min_size update_rect() def on_pan_tl(e: ft.DragUpdateEvent): # top-left nonlocal rect_left, rect_top, rect_width, rect_height rect_left += e.delta_x rect_top += e.delta_y rect_width -= e.delta_x rect_height -= e.delta_y if rect_width < min_size: rect_left -= (min_size - rect_width) rect_width = min_size if rect_height < min_size: rect_top -= (min_size - rect_height) rect_height = min_size update_rect() # ==== ハンドル作成用 ヘルパー ==== def create_handle(on_pan, cursor): return ft.GestureDetector( drag_interval=1, on_pan_update=on_pan, mouse_cursor=cursor, content=ft.Container( width=handle_size, height=handle_size, bgcolor=ft.Colors.WHITE, border=ft.border.all(1, ft.Colors.BLACK), border_radius=handle_size / 2, ink=True, # クリック/ドラッグを受け取る ), ) # ==== クロップ用矩形 ==== crop_rect = ft.Container( ref=rect_ref, left=rect_left, top=rect_top, width=rect_width, height=rect_height, border=ft.border.all(2, ft.Colors.YELLOW), content=ft.Stack( controls=[ # 矩形全体をドラッグして移動 ft.Container( expand=True, content=ft.GestureDetector( on_pan_update=on_pan_move, drag_interval=1, mouse_cursor=ft.MouseCursor.MOVE, content=ft.Container(expand=True), ), ), # 4 つのリサイズハンドル ft.Container( alignment=ft.alignment.top_left, expand=True, content=create_handle(on_pan_tl, ft.MouseCursor.RESIZE_NW), ), ft.Container( alignment=ft.alignment.top_right, expand=True, content=create_handle(on_pan_tr, ft.MouseCursor.RESIZE_NE), ), ft.Container( alignment=ft.alignment.bottom_left, expand=True, content=create_handle(on_pan_bl, ft.MouseCursor.RESIZE_SW), ), ft.Container( alignment=ft.alignment.bottom_right, expand=True, content=create_handle(on_pan_br, ft.MouseCursor.RESIZE_SE), ), ] ), ) # 画像+矩形 を Stack で重ねる stack = ft.Stack( controls=[ ft.Image( src=EXAMPLE_IMAGE, width=400, height=300, fit=ft.ImageFit.CONTAIN, ), crop_rect, ], width=400, height=300, ) page.add(stack) if __name__ == "__main__": ft.app(target=main)ポイント
- 4 つのコーナーごとに
GestureDetectorを配置 on_pan_update内でrect_left/rect_top/rect_width/rect_heightを適宜更新- 最小サイズを超えないようにクランプ処理
- 矩形全体をドラッグして移動できるよう,空の
GestureDetector(MOVEカーソル付き)を重ねて配置
これで,各角のハンドルをドラッグすると自由にクロップ矩形のリサイズ・移動が行えます。
- 4 つのコーナーごとに
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.