前提・実現したいこと
TkinterとOpenCVを使用した、カメラシステムの作成を行っています。
現状、キャプチャを行うボタンおよびシステムの停止を行うボタンは問題なく動作します。
ダイアログを呼び出し、任意のフォルダにキャプチャした画像を保存できる機能を実装できるよう
試行錯誤しているのですが、中々上手くいきません。ご教示の程よろしくお願い致します。
試したこと
保存フォルダ指定ボタンの要素からパスは取得できたが、
キャプチャボタンの要素 imwrite("dir")にパスの反映ができず、
「dir20191203.jpg」の様な形で名前が付けられ保存されるだけである。
該当のソースコード
import os, tkinter, tkinter.filedialog, tkinter.messagebox import cv2 import PIL.Image, PIL.ImageTk import time from datetime import datetime class App: def __init__(self, window, window_title, video_source=0): self.window = window self.window.title(window_title) self.video_source = video_source self.vid = MyVideoCapture(self.video_source) self.canvas = tkinter.Canvas(window, width=1920, height=1080) self.canvas.pack() # キャプチャボタン self.btn_snapshot = tkinter.Button(window, text="Snapshot", font=("",20),width=30,height=3) self.btn_snapshot.place(x=750, y=530) self.btn_snapshot.configure(command=self.snapshot) # システム停止ボタン self.btn_close = tkinter.Button(window, text="Close",font=("",20),width=30,height=2) self.btn_close.place(x=750, y=10) self.btn_close.configure(command=self.destructor) # 保存フォルダ指定ボタン self.btn_savepath = tkinter.Button(window, text="folder", font=("",20),width=15,height=1) self.btn_savepath.place(x=420, y=585) self.btn_savepath.configure(command=self.savepath) self.delay = 15 self.update() self.window.mainloop() #保存フォルダ指定ボタンの要素 def savepath(self): root = tkinter.Tk() root.withdraw() iDir = os.path.abspath(os.path.dirname(__file__)) dir = tkinter.filedialog.askdirectory(initialdir = iDir) print(dir) #キャプチャボタンの要素 def snapshot(self): ret, frame = self.vid.get_frame() if ret: cv2.imwrite("dir" + time.strftime("%Y%m%d%H%M%S") + ".jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) #カメラの表示画面 def update(self): ret, frame = self.vid.get_frame() if ret: self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(frame)) self.canvas.create_image(0, 0, image = self.photo, anchor = tkinter.NW) self.window.after(self.delay, self.update) #システム停止ボタンの要素 def destructor(self): self.window.destroy() # ビデオのソース class MyVideoCapture: def __init__(self, video_source=0): self.vid = cv2.VideoCapture(video_source) if not self.vid.isOpened(): raise ValueError("video_sourceを確認", video_source) self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH) self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT) def get_frame(self): if self.vid.isOpened(): ret, frame = self.vid.read() if ret: return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) else: return (ret, None) else: return (ret, None) def __del__(self): if self.vid.isOpened(): self.vid.release() App(tkinter.Tk(), "camera")
補足情報(FW/ツールのバージョンなど)
なし
あなたの回答
tips
プレビュー