質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

5573閲覧

tkinter 表示画像を保存できない

erisawa

総合スコア12

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/07/30 02:49

実現したいこと
tkinterを使って画像のトリミングを行いたい。スライドバーで線を動かし、線で囲った部分の画像を保存したいのですが、エラーがでてきてしまい、保存できません。

エラーコード

During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:/Users/Desktop/tkinter.py", line 15, in onSaveButton temp_image=Image.open(img) File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\PIL\Image.py", line 2772, in open fp = io.BytesIO(fp.read()) AttributeError: 'PhotoImage' object has no attribute 'read'
import tkinter as tk import tkinter.ttk as ttk from PIL import Image, ImageTk root=tk.Tk() def onSaveButton(): #表示画像を取り込み temp_image=Image.open(img) #選択位置で切り出し cropped_image=temp_image.crop((left.get(), top.get(), right.get(), bottom.get())) #保存 cropped_image.save("c:/Desktop/a.jpg") def onSliderLeft(args): canvas.delete("left_line") canvas.create_line(left.get(), 0, left.get(),500, tag="left_line", fill='green') def onSliderRight(args): canvas.delete("right_line") canvas.create_line(right.get(), 0, right.get(),500, tag="right_line", fill='red') def onSliderTop(args): canvas.delete("top_line") canvas.create_line(0, top.get(), 500,top.get(), tag="top_line", fill='green') def onSliderBottom(args): canvas.delete("bottom_line") canvas.create_line(0, bottom.get(),500,bottom.get(), tag="bottom_line", fill='red') #ウィンドウのタイトルを定義する root.title('a') #ウィンドウサイズを定義する root.geometry('1000x700')#横×縦 #画像opencvからpilに変換 #image_bgr=cv2.imread("c:/temp/0327_/2_1.jpg") #image_rgb=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #image_pil=Image.fromarray(image_rgb) #image_tk=ImageTk.PhotoImage(image_pil) img=Image.open("c:/temp/rabel3.jpg") img=ImageTk.PhotoImage(img) #画像を表示するためのきゃんぱすの作成 canvas=tk.Canvas(root, width=500, height=500) canvas.grid(row=0, column=0) #各種ボタンをつくる #選択領域を保存するボタン button_save=tk.Button(root, text="save", command=onSaveButton) button_save.grid(row=4,column=6) #スライダ用の変数を確保 left=0 right=0 top=0 bottom=0 #画像切り出しようのラインをキャンバスに描画 canvas.create_line(left,0,left,500,fill='green',width=5) canvas.create_line(right,0,right,500,fill='red') canvas.create_line(0,top,500,top,fill='green') canvas.create_line(0,bottom,500,bottom,fill='red') #画像切り出しようのスライダ left=ttk.Scale(root, orient='h',from_=0,to_=500,length=150,command=onSliderLeft) left.grid(row=3,column=0) right=ttk.Scale(root,orient='h',from_=0,to_=500,length=150,command=onSliderRight) right.grid(row=4,column=0) top=ttk.Scale(root, orient='v',from_=0,to_=500,length=150,command=onSliderTop) top.grid(row=4,column=2) bottom=ttk.Scale(root,orient='v',from_=0,to_=500,length=150,command=onSliderBottom) bottom.grid(row=4,column=3) #画像切り出しようのラインを表示している画像に合わせて移動 left.set(0) right.set(0) top.set(0) bottom.set(0) #キャンパスに画像を表示する canvas.create_image(0,0,image=img, anchor='nw' ) root.mainloop()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

img変数にはPhotoImageオブジェクトが代入されていて、
PhotoImageオブジェクトはImage.open関数では開けないからです。

onSaveButton関数処理内のImage.open関数に
始めに画像を表示している処理で指定している画像のパスを渡せば動作します。

python

1image_name="c:/temp/rabel3.jpg" 2 3def onSaveButton(): 4 #表示画像を取り込み 5 temp_image=Image.open(image_name) 6 #選択位置で切り出し 7 cropped_image=temp_image.crop((left.get(), top.get(), right.get(), bottom.get())) 8 #保存 9 cropped_image.save("c:/Desktop/a.jpg")

投稿2020/07/30 03:26

yureighost

総合スコア2183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

erisawa

2020/07/31 06:28

試してみましたが、別のエラーが発生しました。 ''' Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\A171021\AppData\Local\Continuum\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:/Users/A171021/Desktop/tkinter.py", line 21, in onSaveButton cropped_image.save("c:/Desktop/a.jpg") File "C:\Users\A171021\AppData\Local\Continuum\anaconda3\lib\site-packages\PIL\Image.py", line 2081, in save fp = builtins.open(filename, "w+b") FileNotFoundError: [Errno 2] No such file or directory: 'c:/Desktop/a.jpg' '''
erisawa

2020/07/31 06:38

保存先が間違っていたようでした。解決しました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問