質問編集履歴

4

修正

2020/07/24 14:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,9 +18,9 @@
18
18
 
19
19
  ```
20
20
 
21
- 2020-07-24T14:08:32.261401+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
21
+ 2020-07-24T14:08:32.261401+00:00 app[web.1]: return render_template("index.html",message="モザイクアート生成完了", image_url=base64Img)
22
-
22
+
23
- 2020-07-24T14:08:32.261401+00:00 app[web.1]: File "/app/app.py", line 113, in post
23
+ 2020-07-24T14:08:32.261402+00:00 app[web.1]: NameError: name 'base64Img' is not defined
24
24
 
25
25
  ```
26
26
 

3

追記

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
  ```

2

追記

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
+ ```

1

誤字

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