pythonとflaskで作ったモザイクアートを生成するプログラムを作りました。
生成した画像をhtmlで表示させたいのですが、
FileNotFoundError: [Errno 2] No such file or directory: './tmp/output.jpg'
と出てしまいます。
なぜなのでしょうか。
url:https://gentle-savannah-92123.herokuapp.com/
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 print("モザイクアート生成開始") 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 OutputImage="./tmp/output.jpg" 107 Target.save(OutputImage) 108 Target.show() 109 return render_template("index.html",message="モザイクアート生成完了", image_url=OutputImage) 110 111 112if __name__=="__main__": 113 app.run(debug=True) 114
index.html
1<!DOCTYPE html> 2<html lang="jn" dir="ltr"> 3 <head> 4 <meta charset="utf-8"> 5 <title>モザイクアート生成</title> 6 </head> 7 <body> 8 <h1>{{ title }}</h1> 9<p>{{ message }}</p> 10<form action="/" method="POST" enctype="multipart/form-data"> 11 <div> 12 <label for="name">素材の入ったフォルダ:</label> 13 <input type="text" id="source" name="source" placeholder="C:xxxx"> 14 </div> 15 <div> 16 <label for="name">目標画像:</label> 17 <input type="text" id="goal" name="goal" placeholder="C:xxxx"> 18 </div> 19 <div> 20 <input type="submit" value="モザイクアート生成"> 21 </div> 22</form> 23 <img src="{{ image_url }}" /> 24 </body> 25</html> 26