前提・実現したいこと
ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
tkinterを使って画像編集ソフトを作りたいと思っているのですが、編集した画像が保存できなくて困っています。編集前の画像を保存してしまいます。
エラーメッセージ ```無し ### 該当のソースコード ```ここに言語名を入力 python ソースコード ```import os,sys from tkinter import * from tkinter import ttk from tkinter import filedialog from tkinter import messagebox from PIL import Image, ImageTk import cv2 import subprocess import numpy as np import shutil import tkinter.font as tkFont # 顔検出インスタンス生成 cascadePath = 'face.xml' faceCascade = cv2.CascadeClassifier(cascadePath) #クラスを作る class image_gui(): # 変数 filepath = None gamma = None Brightness = None Saturation= None Gaussian = None input_canvas = None output_canvas = None chg_out = None #初期画面設計 def __init__(self, main): # ファイル削除処理 self.file_del() # 参照ボタン配置 button1 = Button(root, text="参照", command=self.button1_clicked) button1.grid(row=0, column=1) button1.place(x=655, y=12) # 閉じるボタン close1 = Button(root,text="閉じる",command=self.close_clicked) close1.grid(row=0,column=3) close1.place(x=715,y=12) # 参照ファイルパス表示ラベルの作成 self.file1 = StringVar() self.file1_entry = ttk.Entry(root,textvariable=self.file1, width=70) self.file1_entry.grid(row=0, column=2) self.file1_entry.place(x=12,y=10) #saveボタン作成 button2 = Button(root, text = "画像を保存して",command = self.save_clicked, width = 8) button2.grid(row = 0, column = 3) button2.place(x = 665, y = 45) #顔検出作成 button3 = Button(root, text = "顔検出 ON", command = self.face_clicked, width = 10, height = 3) button3.grid(row = 0, column = 3) button3.place(x = 75, y = 350) #モザイクボタン作成 button4 = Button(root, text = "モザイク ON", command =self.mosaic_clicked,width = 10, height = 3) button4.grid(row = 0, column = 3) button4.place(x = 220, y = 350) #ファイルを削除する def file_del(self): if os.path.exists("output_image_small.png") == True: os.remove("output_image_small.png") if os.path.exists("output_image.jpeg") == True: os.remove("output_image.jpeg") if os.path.exists("input_image.png") == True: os.remove("input_image.png") if os.path.exists("input_image_file.jpeg") == True: os.remove("input_image_file.jpeg") #閉じるメソッド def close_clicked(self): res = messagebox.askokcancel("確認しろ", "閉じる??") if res != True: # 処理終了 return self.file_del() #処理終了 sys.exit() #参照ボタン def button1_clicked(self): fTyp = [("画像ファイル","*.jpeg"),("画像ファイル","*.jpg")] iDir = os.path.abspath(os.path.dirname(__file__)) # path取得 self.filepath = filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir) # ファイル選択指定しろ if self.filepath == "": return self.file1.set(self.filepath) #画像を表示する img = cv2.imread(self.filepath) cv2.imwrite("input_image_file.jpeg",img) img2 = cv2.resize(img,dsize=(320,240)) cv2.imwrite("input_image.png",img2) self.out_image = ImageTk.PhotoImage(file="input_image.png") input_canvas.create_image(163, 122, image=self.out_image) #font label fontstyle = tkFont.Font(family = "Lucida Grande", size = 20) label1 = Label(root, text = "Before", font = fontstyle) label1.grid(row = 0, column = 3) label1.place(x = 130, y = 60) #save処理ボタン def save_clicked(self): f_type = [("画像ファイル", "*.jpeg"),("画像ファイル", "*.jpg")] # 実行中のフォルダパス取得 ini_dir = os.getcwd() # ファイル保存のダイアログ出力 filepath = filedialog.asksaveasfilename(filetypes=f_type , initialdir=ini_dir, title="適当に保存") # ファイル名を取得 filename = os.path.basename(filepath) # ファイルを保存 if filepath: # ファイルを書き込みで開く with open(filepath, "w", encoding="utf_8") as f: len = f.write(filename) else: return # 編集した画像ファイル確認する if os.path.exists("output_image.png") == True: # 編集した画像ファイルを、ダイアログで指定したファイルへコピーする shutil.copyfile("output_image.png", filepath) else: # 編集画像が無いので、入力した画像ファイルで保存する。 shutil.copyfile("input_image_file.jpeg", filepath) #顔検出ボタン処理 def face_clicked(self): # 出力ファイルなし if os.path.exists("output_image.jpeg") == False: # 入力ファイルの読み出し img = cv2.imread(self.filepath) else: # 作成済みの出力ファイル img = cv2.imread("output_image.png") # 顔検出と描画する img = self.face_detect(img) # 表示用に画像サイズを小さくする img2 = cv2.resize(img,dsize=(320,240)) # 出力画像を保存 cv2.imwrite("output_face_image.png",img2) # 画像をセット self.out_image3 = ImageTk.PhotoImage(file="output_face_image.png") output_canvas.create_image(160, 120, image=self.out_image3) os.remove("output_face_image.png") fontstyle = tkFont.Font(family = "Lucida Grande", size = 20) label2 = Label(root, text = "After", font = fontstyle) label2.grid(row = 0, column = 3) label2.place(x = 580, y = 60) #顔検出の処理 def face_detect(self,img): # グレースケールに変換 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 顔検出 faces = faceCascade.detectMultiScale( gray, scaleFactor = 1.2, minNeighbors = 3, minSize = (10, 10) ) for(x,y,w,h) in faces: # 四角で加工 cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2) return img #モザイクをかけるメソッド def mosaic_clicked(self): if os.path.exists("output_image.jpeg") == False: img = cv2.imread(self.filepath) else: img = cv2.imread("output_image.jpeg") img = self.face_detect_mosaic(img) img2 = cv2.resize(img, dsize = (320,240)) cv2.imwrite("output_mosaic_image.png", img2) self.out_image2 = ImageTk.PhotoImage(file = "output_mosaic_image.png") output_canvas.create_image(160,120,image = self.out_image2) os.remove("output_mosaic_image.png") #font label1 fontstyle = tkFont.Font(family = "Lucida Grande", size = 20) label2 = Label(root, text = "After", font = fontstyle) label2.grid(row = 0, column = 3) label2.place(x = 580, y = 60) def face_detect_mosaic(self,img): #color gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #顔検出 faces = faceCascade.detectMultiScale( gray, scaleFactor = 1.2, minNeighbors = 3, minSize = (10, 10) ) for(x,y,w,h) in faces: # モザイクをかける img = self.mosaic(img,(x,y,x+w,y+h),10) return img #mosaicをかける def mosaic(self,img,rect,size): # モザイクをかける領域を取得 (x1,y1,x2,y2)=rect w=x2-x1 h=y2-y1 i_rect = img[y1:y2,x1:x2] # 一度縮小して拡大する i_small = cv2.resize(i_rect,(size,size)) i_mos = cv2.resize(i_small,(w,h),interpolation=cv2.INTER_AREA) img2=img.copy() img2[y1:y2,x1:x2]=i_mos return img2 #全体の設定をします if __name__ == '__main__': root = Tk() root.title("Image Viewer") # GUI全体のフレームサイズ root.geometry("770x400") # 出力ファイル画像表示の場所指定とサイズ指定 output_canvas = Canvas(root, width=320, height=240) output_canvas.place(x=440, y=90) # 入力ファイル画像表示の場所指定とサイズ指定 input_canvas = Canvas(root, width=320, height=240) input_canvas.place(x=5, y=90) # GUI表示 image_gui(root) root.mainloop() ### 試したこと ファイルが前提として無いのか?と思いterminalで確認した所imwriteで保存したはずなのにファイルがないと表示され、ファイル名や拡張子などをいろいろ変えても保存されていませんでした。 ### 補足情報(FW/ツールのバージョンなど) Mac book air 2020 atom
インデントがなくなりコードが読めません(実行できません)。以下を参考にコードを提示ください。
https://teratail.storage.googleapis.com/uploads/contributed_images/56957fe805d9d7befa7dba6a98676d2b.gif
あなたの回答
tips
プレビュー