teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの修正

2020/05/14 04:46

投稿

tomo754
tomo754

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,19 +1,14 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
 
4
- pythonにて任意の画像をCanvas内にグレースケールで表示させ、その画像の中心70×70で切り出して、別に用意したCanvas2に2倍に拡大して表示させたい。
4
+ pythonにて任意の画像をCanvas内にグレースケールで表示させ、その画像の左上座標(20, 20, 70, 70)50×50で切り出して、別に用意したCanvas2に2倍に拡大して表示させたい。
5
- コードを実行すると以下のエラーメッセージが発生ました。
5
+ しかし、コードを実行すると切り出した画像では無く、元画像が表示されている様子
6
6
  初心者で理解が浅く申し訳ありませんがご教授下さい。
7
7
 
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
- Traceback (most recent call last):
11
- File "C:\Users\0020215025\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
12
- return self.func(*args)
13
- File "c:/Users/0020215025/Documents/python_prg/動作確認.py", line 68, in button4_click
14
- clipped = image[180, 125, 230, 175]
15
- UnboundLocalError: local variable 'image' referenced before assignment
16
10
 
11
+
17
12
  ### 該当のソースコード
18
13
 
19
14
  python
@@ -27,9 +22,6 @@
27
22
  import os
28
23
  import matplotlib.pyplot as plt
29
24
 
30
- def __init__(self, main):
31
- #ファイル削除処理
32
- self.file_del()
33
25
 
34
26
 
35
27
 
@@ -46,27 +38,23 @@
46
38
  label2 = tk.Label(text='ファイル名:')
47
39
  label2.place(x=10, y=40)
48
40
  #ファイルパスの表示欄を作成
49
- input_box1 = tk.Entry(width=75)
41
+ input_box1 = tk.Entry(width=75, bd=4)
50
42
  input_box1.place(x=80, y=40)
51
43
 
44
+ img = None #初期化
52
45
 
53
-
54
46
  #参照ボタンの動作
55
- def button1_clicked(self):
56
- #ファイルパスを取得
57
- idir = r'C:\descktop'
58
- filepath = tk.filedialog.askdirectory(initialdir = idir)
59
-
60
-
61
47
  def file_select():
62
48
  #ファイルパスを表示欄に表示
63
49
  idir = r'C:\descktop'
64
50
  filetype = [("すべて","*")]
65
51
  filepath = tk.filedialog.askopenfilename(filetypes = filetype, initialdir = idir)
52
+ input_box1.delete(0, tk.END)
66
- input_box1.insert(tk.END, filepath)
53
+ input_box1.insert(0, filepath)
67
54
  #選択したファイルを表示
68
55
  if filepath and os.path.isfile(filepath):
69
56
  #PILで開きグレースケールに変換
57
+ global img #グローバル宣言
70
58
  img = Image.open(filepath).convert("L")
71
59
  image = ImageTk.PhotoImage(image=img)
72
60
  #画像の情報を取得
@@ -74,66 +62,75 @@
74
62
  #画像リサイズ
75
63
  image = ImageTk.PhotoImage(img.resize((int(h/10),int(w/10))))
76
64
 
77
- #画像をCanvas1に表示
65
+ #表示位置調整
78
66
  cv.image = image
79
-
80
- #表示位置調整
81
67
  cv.create_image(image.width()/2, image.height()/2, image=image)
82
68
 
83
69
  #範囲表示ボタンの動作
84
70
  def button3_click():
85
- cv.create_rectangle([(180, 125), (230, 175)], outline = "red")#中央
71
+ cv.create_rectangle([(20, 20), (70, 70)], outline = "red")#左上
86
72
 
87
73
  #切り出しボタンの動作
88
74
  def button4_click():
75
+ if img is not None: #値の確認
89
- clipped = image[180, 125, 230, 175]#矩形と同じサイズで切り出し
76
+ cropped_img = img.crop((20, 20, 70, 70))
90
- #画像の情報を取得
77
+ #画像の情報を取得
91
- h, w = img.size[:]
78
+ h, w = cropped_img.size[:]
92
- #画像リサイズ
79
+ #画像リサイズ
93
- image = ImageTk.PhotoImage(img.resize((int(h*2),int(w*2))))
80
+ cropped_img = ImageTk.PhotoImage(img.resize((int(h*2),int(w*2))))
94
81
 
95
- #画像をCanvas1に表示
82
+ #表示位置調整
96
- cv2.image = image
83
+ cv2.image = cropped_img
84
+ cv2.create_image(cropped_img.width()/2, cropped_img.height()/2, image=cropped_img)
97
85
 
98
- #表示位置調整
86
+
99
- cv2.create_image(image.width()/2, image.height()/2, image=image)
100
87
 
101
88
  #Canvasの作成(メイン画像)
102
89
  cv = tk.Canvas(win, width=409, height=300, bg="white",)
103
90
  cv.place(x=10, y=70)
104
91
 
105
92
  #切り抜いた画像の表示
106
- #ラベル3を作成
93
+ #ラベル3を作成(左上画像)
107
- label3 = tk.Label(text='●中心画像')
94
+ label3 = tk.Label(text='●左上画像')
108
95
  label3.place(x=450, y=110)
109
96
 
110
- #Canvas2の作成
97
+ #Canvasの作成(左上画像)
111
- cv2 = tk.Canvas(win, width=140, height=140, bg="white",)
98
+ cv2 = tk.Canvas(win, width=100, height=100, bg="white",)
112
99
  cv2.place(x=450, y=130)
113
100
 
101
+ #ラベル4を作成(右上画像)
102
+ label4 = tk.Label(text='●右上画像')
103
+ label4.place(x=630, y=110)
114
104
 
105
+ #Canvasの作成(右上画像)
106
+ cv3 = tk.Canvas(win, width=100, height=100, bg="white",)
107
+ cv3.place(x=630, y=130)
115
108
 
116
109
  #参照ボタン1を作成
117
- button1 = tk.Button(text="参照", command=file_select, width=8)
110
+ button1 = tk.Button(text="参照", command=file_select, width=8, bd=4)
118
111
  button1.place(x=630, y=35)
119
112
 
120
113
  #閉じるボタンを作成
121
- button2 = tk.Button(text="閉じる", command=win.destroy, width=8)
114
+ button2 = tk.Button(text="閉じる", command=win.destroy, width=8, bd=4)
122
115
  button2.place(x=730, y=35)
123
116
 
124
117
  #切り出し範囲表示ボタンを作成
125
- button3 = tk.Button(text="範囲表示", command=button3_click, width=10)
118
+ button3 = tk.Button(text="範囲表示", command=button3_click, width=10, bd=4)
126
119
  button3.place(x=450, y=70)
127
120
 
128
121
  #切り出し動作ボタンを作成
129
- button4 = tk.Button(text="切り出し", command=button4_click, width=10)
122
+ button4 = tk.Button(text="切り出し", command=button4_click, width=10, bd=4)
130
123
  button4.place(x=540, y=70)
131
124
 
125
+
126
+
127
+
128
+
129
+
132
- #ウインドウ動かす
130
+ #GUIそのまま表示
133
131
  win.mainloop()
134
132
 
135
133
  ```
136
- ```
137
134
 
138
135
  ### 試したこと
139
136