質問編集履歴

4

修正

2020/07/24 14:14

投稿

削除済ユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,13 +18,9 @@
18
18
 
19
19
  ```
20
20
 
21
- 2020-07-23T16:05:23.807421+00:00 app[web.1]: "The view function did not return a valid response. The"
22
-
23
- 2020-07-23T16:05:23.807448+00:00 app[web.1]: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
24
-
25
- 2020-07-23T16:05:23.807722+00:00 heroku[router]: at=info method=POST path="/" host=mosaicart.herokuapp.com request_id=7c967a70-52ac-407e-90b2-306db0ad6922 fwd="180.145.10.214" dyno=web.1 connect=1ms service=435ms status=500 bytes=470 protocol=https
26
-
27
- 2020-07-23T16:05:23.808997+00:00 app[web.1]: 10.45.252.73 - - [23/Jul/2020:16:05:23 +0000] "POST / HTTP/1.1" 500 290 "https://mosaicart.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
21
+ 2020-07-24T14:08:32.261401+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
22
+
23
+ 2020-07-24T14:08:32.261401+00:00 app[web.1]: File "/app/app.py", line 113, in post
28
24
 
29
25
  ```
30
26
 
@@ -256,9 +252,11 @@
256
252
 
257
253
  Target.save(buffer, "PNG")
258
254
 
255
+ global base64Img
256
+
259
257
  base64Img=base64.b64encode(buffer.getvalue()).decode("utf-8")
260
258
 
261
- return render_template("index.html",message="モザイクアート生成完了", image_url=base64Img)
259
+ return render_template("index.html",message="モザイクアート生成完了", image_url=base64Img)
262
260
 
263
261
 
264
262
 
@@ -268,4 +266,6 @@
268
266
 
269
267
  app.run()
270
268
 
269
+
270
+
271
271
  ```

3

追記

2020/07/24 14:10

投稿

削除済ユーザー
test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,245 @@
27
27
  2020-07-23T16:05:23.808997+00:00 app[web.1]: 10.45.252.73 - - [23/Jul/2020:16:05:23 +0000] "POST / HTTP/1.1" 500 290 "https://mosaicart.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ 以下がコードです。
34
+
35
+
36
+
37
+ ```python
38
+
39
+ from flask import Flask, render_template, request, send_file
40
+
41
+ from PIL import Image,ImageFilter
42
+
43
+ import subprocess
44
+
45
+ import os
46
+
47
+ import random
48
+
49
+ import math
50
+
51
+ import time
52
+
53
+ import numpy as np
54
+
55
+ import glob
56
+
57
+ import cv2
58
+
59
+ import io
60
+
61
+ import base64
62
+
63
+
64
+
65
+
66
+
67
+ app = Flask(__name__)
68
+
69
+
70
+
71
+ # getのときの処理
72
+
73
+ @app.route('/', methods=['GET'])
74
+
75
+ def get():
76
+
77
+ return render_template('index.html', \
78
+
79
+ title = 'モザイクアートジェネレーター', \
80
+
81
+ message = '設定してください')
82
+
83
+
84
+
85
+ # postのときの処理
86
+
87
+ @app.route('/', methods=['POST'])
88
+
89
+ def post():
90
+
91
+ SourceDir = request.form['source']
92
+
93
+ GoalImage = request.files["goal"]
94
+
95
+ OutputImage="./output.jpg"
96
+
97
+ SourceImageSize=(40,30)
98
+
99
+
100
+
101
+ TargetZoom=11
102
+
103
+
104
+
105
+ used_count = 3
106
+
107
+
108
+
109
+
110
+
111
+ def __create_tile(org_image,height,width):
112
+
113
+
114
+
115
+ w, h = org_image.size
116
+
117
+ RectList=[]
118
+
119
+ for y in range(0, math.floor(h / height) + 1):
120
+
121
+ for x in range(0, math.floor(w / width) + 1):
122
+
123
+ height2 = y * height
124
+
125
+ width2 = x * width
126
+
127
+ crap_img = org_image.crop((width2, height2, width2 + width, height2 + height))
128
+
129
+ RectList.append((crap_img,width2,height2))
130
+
131
+ return RectList
132
+
133
+
134
+
135
+ def __clac_hist(img):
136
+
137
+
138
+
139
+ hist_list = []
140
+
141
+ color = ['r','g','b']
142
+
143
+ images = np.asarray(img)
144
+
145
+
146
+
147
+ for i in enumerate(color):
148
+
149
+ hist_list.append(cv2.calcHist([images],[i[0]],None,[256],[0,256]))
150
+
151
+ return hist_list
152
+
153
+
154
+
155
+ if __name__=="__main__":
156
+
157
+
158
+
159
+ startTime=time.time()
160
+
161
+
162
+
163
+
164
+
165
+ target=Image.open(GoalImage)
166
+
167
+ target=target.resize(tuple(math.floor(i*TargetZoom) for i in target.size))
168
+
169
+
170
+
171
+
172
+
173
+ tiles = __create_tile(target,SourceImageSize[1],SourceImageSize[0])
174
+
175
+
176
+
177
+ file_paths = glob.glob(SourceDir+"\\*")
178
+
179
+ src_hist_dict = {}
180
+
181
+ for file_path in file_paths:
182
+
183
+ file_name = file_path.split("\\")[-1]
184
+
185
+
186
+
187
+ try:
188
+
189
+ src_image = Image.open(file_path).resize(SourceImageSize)
190
+
191
+ except Exception:
192
+
193
+ continue
194
+
195
+ src_hist_dict[file_name] = [src_image,__clac_hist(src_image),0]
196
+
197
+
198
+
199
+
200
+
201
+ Target=Image.new("RGB",target.size,255)
202
+
203
+ while(len(tiles) > 0):
204
+
205
+ result = []
206
+
207
+
208
+
209
+ r=random.randrange(len(tiles))
210
+
211
+ tileRect=tiles[r]
212
+
213
+ del tiles[r]
214
+
215
+
216
+
217
+ tile_hist = __clac_hist(tileRect[0])
218
+
219
+
220
+
221
+ tile_hist = np.array(tile_hist)
222
+
223
+ tile_hist = tile_hist.reshape(tile_hist.shape[0] * tile_hist.shape[1], 1)
224
+
225
+
226
+
227
+
228
+
229
+ for file_name,src_hist in src_hist_dict.items():
230
+
231
+ src_hist = np.array(src_hist[1])
232
+
233
+ src_hist = src_hist.reshape(src_hist.shape[0] * src_hist.shape[1], 1)
234
+
235
+
236
+
237
+ d = cv2.compareHist(tile_hist, src_hist, cv2.HISTCMP_INTERSECT)
238
+
239
+ result.append([d,file_name])
240
+
241
+ result.sort(reverse=True)
242
+
243
+
244
+
245
+ Target.paste(src_hist_dict[result[0][1]][0],(tileRect[1],tileRect[2]))
246
+
247
+
248
+
249
+ src_hist_dict[result[0][1]][2] = src_hist_dict[result[0][1]][2] + 1
250
+
251
+
252
+
253
+
254
+
255
+ buffer=io.BytesIO()
256
+
257
+ Target.save(buffer, "PNG")
258
+
259
+ base64Img=base64.b64encode(buffer.getvalue()).decode("utf-8")
260
+
261
+ return render_template("index.html",message="モザイクアート生成完了", image_url=base64Img)
262
+
263
+
264
+
265
+
266
+
267
+ if __name__=="__main__":
268
+
269
+ app.run()
270
+
271
+ ```

2

追記

2020/07/24 03:55

投稿

削除済ユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,8 @@
1
1
  pythonとflaskで書いたコードをherokuでデプロイしました。
2
2
 
3
+ ローカル環境ではうまく動いていましたが、
4
+
3
- しかし、500 Internet Server Errorとなり、
5
+ 500 Internal Server Errorとなり、
4
6
 
5
7
  うまく動きませんでした。
6
8
 

1

誤字

2020/07/23 16:24

投稿

削除済ユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,6 @@
1
1
  pythonとflaskで書いたコードをherokuでデプロイしました。
2
2
 
3
- ローカル環境ではうまく動いていましたが、
4
-
5
- 500 Internal Server Errorとなり、
3
+ しかし、500 Internet Server Errorとなり、
6
4
 
7
5
  うまく動きませんでした。
8
6
 
@@ -18,254 +16,12 @@
18
16
 
19
17
  ```
20
18
 
21
- 2020-07-24T14:08:32.261401+00:00 app[web.1]: return render_template("index.html",message="モザイクアート生成完了", image_url=base64Img)
19
+ 2020-07-23T16:05:23.807421+00:00 app[web.1]: "The view function did not return a valid response. The"
22
20
 
23
- 2020-07-24T14:08:32.261402+00:00 app[web.1]: NameError: name 'base64Img' is not defined
21
+ 2020-07-23T16:05:23.807448+00:00 app[web.1]: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
22
+
23
+ 2020-07-23T16:05:23.807722+00:00 heroku[router]: at=info method=POST path="/" host=mosaicart.herokuapp.com request_id=7c967a70-52ac-407e-90b2-306db0ad6922 fwd="180.145.10.214" dyno=web.1 connect=1ms service=435ms status=500 bytes=470 protocol=https
24
+
25
+ 2020-07-23T16:05:23.808997+00:00 app[web.1]: 10.45.252.73 - - [23/Jul/2020:16:05:23 +0000] "POST / HTTP/1.1" 500 290 "https://mosaicart.herokuapp.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
24
26
 
25
27
  ```
26
-
27
-
28
-
29
- 以下がコードです。
30
-
31
-
32
-
33
- ```python
34
-
35
- from flask import Flask, render_template, request, send_file
36
-
37
- from PIL import Image,ImageFilter
38
-
39
- import subprocess
40
-
41
- import os
42
-
43
- import random
44
-
45
- import math
46
-
47
- import time
48
-
49
- import numpy as np
50
-
51
- import glob
52
-
53
- import cv2
54
-
55
- import io
56
-
57
- import base64
58
-
59
-
60
-
61
-
62
-
63
- app = Flask(__name__)
64
-
65
-
66
-
67
- # getのときの処理
68
-
69
- @app.route('/', methods=['GET'])
70
-
71
- def get():
72
-
73
- return render_template('index.html', \
74
-
75
- title = 'モザイクアートジェネレーター', \
76
-
77
- message = '設定してください')
78
-
79
-
80
-
81
- # postのときの処理
82
-
83
- @app.route('/', methods=['POST'])
84
-
85
- def post():
86
-
87
- SourceDir = request.form['source']
88
-
89
- GoalImage = request.files["goal"]
90
-
91
- OutputImage="./output.jpg"
92
-
93
- SourceImageSize=(40,30)
94
-
95
-
96
-
97
- TargetZoom=11
98
-
99
-
100
-
101
- used_count = 3
102
-
103
-
104
-
105
-
106
-
107
- def __create_tile(org_image,height,width):
108
-
109
-
110
-
111
- w, h = org_image.size
112
-
113
- RectList=[]
114
-
115
- for y in range(0, math.floor(h / height) + 1):
116
-
117
- for x in range(0, math.floor(w / width) + 1):
118
-
119
- height2 = y * height
120
-
121
- width2 = x * width
122
-
123
- crap_img = org_image.crop((width2, height2, width2 + width, height2 + height))
124
-
125
- RectList.append((crap_img,width2,height2))
126
-
127
- return RectList
128
-
129
-
130
-
131
- def __clac_hist(img):
132
-
133
-
134
-
135
- hist_list = []
136
-
137
- color = ['r','g','b']
138
-
139
- images = np.asarray(img)
140
-
141
-
142
-
143
- for i in enumerate(color):
144
-
145
- hist_list.append(cv2.calcHist([images],[i[0]],None,[256],[0,256]))
146
-
147
- return hist_list
148
-
149
-
150
-
151
- if __name__=="__main__":
152
-
153
-
154
-
155
- startTime=time.time()
156
-
157
-
158
-
159
-
160
-
161
- target=Image.open(GoalImage)
162
-
163
- target=target.resize(tuple(math.floor(i*TargetZoom) for i in target.size))
164
-
165
-
166
-
167
-
168
-
169
- tiles = __create_tile(target,SourceImageSize[1],SourceImageSize[0])
170
-
171
-
172
-
173
- file_paths = glob.glob(SourceDir+"\\*")
174
-
175
- src_hist_dict = {}
176
-
177
- for file_path in file_paths:
178
-
179
- file_name = file_path.split("\\")[-1]
180
-
181
-
182
-
183
- try:
184
-
185
- src_image = Image.open(file_path).resize(SourceImageSize)
186
-
187
- except Exception:
188
-
189
- continue
190
-
191
- src_hist_dict[file_name] = [src_image,__clac_hist(src_image),0]
192
-
193
-
194
-
195
-
196
-
197
- Target=Image.new("RGB",target.size,255)
198
-
199
- while(len(tiles) > 0):
200
-
201
- result = []
202
-
203
-
204
-
205
- r=random.randrange(len(tiles))
206
-
207
- tileRect=tiles[r]
208
-
209
- del tiles[r]
210
-
211
-
212
-
213
- tile_hist = __clac_hist(tileRect[0])
214
-
215
-
216
-
217
- tile_hist = np.array(tile_hist)
218
-
219
- tile_hist = tile_hist.reshape(tile_hist.shape[0] * tile_hist.shape[1], 1)
220
-
221
-
222
-
223
-
224
-
225
- for file_name,src_hist in src_hist_dict.items():
226
-
227
- src_hist = np.array(src_hist[1])
228
-
229
- src_hist = src_hist.reshape(src_hist.shape[0] * src_hist.shape[1], 1)
230
-
231
-
232
-
233
- d = cv2.compareHist(tile_hist, src_hist, cv2.HISTCMP_INTERSECT)
234
-
235
- result.append([d,file_name])
236
-
237
- result.sort(reverse=True)
238
-
239
-
240
-
241
- Target.paste(src_hist_dict[result[0][1]][0],(tileRect[1],tileRect[2]))
242
-
243
-
244
-
245
- src_hist_dict[result[0][1]][2] = src_hist_dict[result[0][1]][2] + 1
246
-
247
-
248
-
249
-
250
-
251
- buffer=io.BytesIO()
252
-
253
- Target.save(buffer, "PNG")
254
-
255
- global base64Img
256
-
257
- base64Img=base64.b64encode(buffer.getvalue()).decode("utf-8")
258
-
259
- return render_template("index.html",message="モザイクアート生成完了", image_url=base64Img)
260
-
261
-
262
-
263
-
264
-
265
- if __name__=="__main__":
266
-
267
- app.run()
268
-
269
-
270
-
271
- ```