前提・実現したいこと
python GUInに設けたcanvasに任意の画像を表示させます。
その表示させた画像をマウス操作で下記動作をさせたい。
マウスホイール:画像の拡大縮小
左クリックドラック:画像の移動
右クリック:画像を初期状態に戻す
該当のソースコード
python
1#Tkinterのライブラリを取り込む 2import tkinter as tk 3from tkinter import filedialog 4import cv2 5from PIL import Image, ImageTk 6import os 7 8def __init__(self, main): 9 #ファイル削除処理 10 self.file_del() 11 12 13 14#ウインドウを作成 15win = tk.Tk() 16win.title("FLIRカメラチルト確認")#タイトル 17win.geometry("700x500")#サイズ 18 19#パーツを配置 20#ラベル1を作成 21label1 = tk.Label(text='■画像読込') 22label1.place(x=10, y=10) 23#ラベル2を作成 24label2 = tk.Label(text='ファイル名:') 25label2.place(x=10, y=40) 26#ファイルパスの表示欄を作成 27input_box1 = tk.Entry(width=75) 28input_box1.place(x=80, y=40) 29 30 31 32#参照ボタンの動作 33def button1_clicked(self): 34 #ファイルパスを取得 35 idir = r'C:\descktop' 36 filepath = tk.filedialog.askdirectory(initialdir = idir) 37 38 39def file_select(): 40 #ファイルパスを表示欄に表示 41 idir = r'C:\descktop' 42 filetype = [("すべて","*")] 43 filepath = tk.filedialog.askopenfilename(filetypes = filetype, initialdir = idir) 44 input_box1.insert(tk.END, filepath) 45 #選択したファイルを表示 46 if filepath and os.path.isfile(filepath): 47 #PILで開きグレースケールに変換 48 img = Image.open(filepath).convert("L") 49 image = ImageTk.PhotoImage(image=img) 50 #画像の情報を取得 51 h, w = img.size[:] 52 #画像リサイズ 53 image = ImageTk.PhotoImage(img.resize((int(h/10),int(w/10)))) 54 55 #画像をCanvasに表示 56 cv.image = image 57 58 #表示位置調整 59 cv.create_image(image.width()/2, image.height()/2, image=image) 60 61 62#Canvasの作成 63cv = tk.Canvas(win, width=409, height=300, bg="white",) 64cv.place(x=10, y=70) 65 66 67#参照ボタン1を作成 68button1 = tk.Button(text="参照", command=file_select, width=8) 69button1.place(x=630, y=35) 70 71#閉じるボタンを作成 72button2 = tk.Button(text="閉じる", command=win.destroy, width=8) 73button2.place(x=630, y=450) 74 75 76 77 78 79#ウインドウを動かす 80win.mainloop() 81
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
python3.6.1
opencv-python4.2.0.34
Pillow7.1.1
windows8
あなたの回答
tips
プレビュー