モザイクアートを作成するプログラムをpythonとflaskで作り、herokuでデプロイしました.
最初の画面は表示されるのですが、モザイクアートを生成を押すとInternal Server Error が出てしまいます.
ログを見ると
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
と出ていました.
Python
1from flask import Flask, render_template, request 2from PIL import Image,ImageFilter 3import subprocess 4import os 5import random 6import math 7import time 8import numpy as np 9import glob 10import cv2 11 12app = Flask(__name__) 13 14# getのときの処理 15@app.route('/', methods=['GET']) 16def get(): 17 return render_template('index.html', \ 18 title = 'モザイクアートを作ろう', \ 19 message = '設定してください') 20 21# postのときの処理 22@app.route('/', methods=['POST']) 23def post(): 24 SourceDir = request.form['source'] 25 GoalImage = request.form["goal"] 26 OutputImage="output.jpg" 27 SourceImageSize=(60,40) 28 29 TargetZoom=11 30 31 used_count = 3 32 33 34 def __create_tile(org_image,height,width): 35 36 w, h = org_image.size 37 RectList=[] 38 for y in range(0, math.floor(h / height) + 1): 39 for x in range(0, math.floor(w / width) + 1): 40 height2 = y * height 41 width2 = x * width 42 crap_img = org_image.crop((width2, height2, width2 + width, height2 + height)) 43 RectList.append((crap_img,width2,height2)) 44 return RectList 45 46 def __clac_hist(img): 47 48 hist_list = [] 49 color = ['r','g','b'] 50 images = np.asarray(img) 51 52 for i in enumerate(color): 53 hist_list.append(cv2.calcHist([images],[i[0]],None,[256],[0,256])) 54 return hist_list 55 56 if __name__=="__main__": 57 58 startTime=time.time() 59 60 61 target=Image.open(GoalImage) 62 target=target.resize(tuple(math.floor(i*TargetZoom) for i in target.size)) 63 64 65 tiles = __create_tile(target,SourceImageSize[1],SourceImageSize[0]) 66 67 file_paths = glob.glob(SourceDir+"\*") 68 src_hist_dict = {} 69 for file_path in file_paths: 70 file_name = file_path.split("\")[-1] 71 72 try: 73 src_image = Image.open(file_path).resize(SourceImageSize) 74 except Exception: 75 continue 76 src_hist_dict[file_name] = [src_image,__clac_hist(src_image),0] 77 78 79 Target=Image.new("RGB",target.size,255) 80 81 while(len(tiles) > 0): 82 result = [] 83 84 r=random.randrange(len(tiles)) 85 tileRect=tiles[r] 86 del tiles[r] 87 88 tile_hist = __clac_hist(tileRect[0]) 89 90 tile_hist = np.array(tile_hist) 91 tile_hist = tile_hist.reshape(tile_hist.shape[0] * tile_hist.shape[1], 1) 92 93 94 for file_name,src_hist in src_hist_dict.items(): 95 src_hist = np.array(src_hist[1]) 96 src_hist = src_hist.reshape(src_hist.shape[0] * src_hist.shape[1], 1) 97 98 d = cv2.compareHist(tile_hist, src_hist, cv2.HISTCMP_INTERSECT) 99 result.append([d,file_name]) 100 result.sort(reverse=True) 101 102 Target.paste(src_hist_dict[result[0][1]][0],(tileRect[1],tileRect[2])) 103 104 src_hist_dict[result[0][1]][2] = src_hist_dict[result[0][1]][2] + 1 105 106 107 Target.show() 108 return render_template("index.html",message="モザイクアート生成完了") 109 110 111if __name__=="__main__": 112 app.run() 113
(追記)
return render_template("index.html",message="モザイクアート生成完了")
のインデントを一つ下げるとエラーは出なくなりました.
しかし、Target.show()
ができません.
なぜなのでしょうか
あなたの回答
tips
プレビュー