Python
1from PIL import Image 2import os 3 4import datetime 5#保存されるファイル名に日付を付ける 6t_delta = datetime.timedelta(hours=9) 7JST = datetime.timezone(t_delta, 'JST') 8now = datetime.datetime.now(JST) 9 10import tkinter 11from tkinter import filedialog 12from tkinter import Tk 13from tkinter import ttk 14from tkinter import messagebox 15img = '' 16#画像を開く処理 17def input_get(): 18 19 typ = [('', '*')] 20 dir = 'C:\\pg' 21 img_file = filedialog.askopenfilenames(filetypes = typ, initialdir = dir) 22 23 24 # リサイズ前の画像を読み込み 25 img = Image.open(img_file[0]) 26#実行ボタンの処理 27def exe(): 28 (width, height) = (int(txt2.get()),int(txt1.get())) 29 img_resized = img.resize((width, height)) 30 output = txt3.get() 31 32 if(output not in 'jpg')or(output not in 'png'): 33 output = output +'.png' 34 img_resized.save(now.strftime('%Y%m%d%H%M%S') + output, quality=90) 35 36 37#ウインドウの処理 38root = tkinter.Tk() 39 40root.geometry('640x380') 41root.title('input window') 42 43 44lbl = tkinter.Label(text='height') 45lbl.place(relx=0.1, rely=0.2) 46 47txt1 = tkinter.Entry(root) 48txt1.place(relx=0.3, rely=0.2, width=150, height=30) 49 50 51lbl = tkinter.Label(text='width') 52lbl.place(relx=0.1, rely=0.4) 53 54txt2 = tkinter.Entry(root) 55txt2.place(relx=0.3, rely=0.4, width=150, height=30) 56 57 58lbl = tkinter.Label(text='file name') 59lbl.place(relx=0.1, rely=0.6) 60 61txt3 = tkinter.Entry(root) 62txt3.place(relx=0.3, rely=0.6, width=150, height=30) 63#画像開く 64btn = tkinter.Button(root, text="Open Image", height = 4,width = 16,command=input_get) 65btn.place(relx=0.05, rely=0.8, width=150, height=40) 66#実行ボタン 67btn = tkinter.Button(root, text="Execute", height = 4,width = 16,command=exe) 68btn.place(relx=0.3, rely=0.8, width=150, height=40) 69 70root.mainloop()
画像をリサイズするプログラムをGUIで動かそうとしています。
仕組みとしてはテキストボックスに横と縦の長さと保存するファイル名を入力してopenimageボタンを押し画像ファイルを選択して、executeボタンでリサイズ実行という流れです。
input_get()関数の中にある
img = Image.open(img_file[0])のimgの中身をexe()関数に持っていきたいのですが上手くいきません。
現状このまま実行すると
AttributeError: 'str' object has no attribute 'resize'
というエラーが出ます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/13 10:21
2022/05/13 10:58
2022/05/13 13:03
2022/05/14 03:20