|
|
import os
|
|
|
import random
|
|
|
|
|
|
import cv2
|
|
|
from flask import Flask, request, send_from_directory, render_template, jsonify, make_response
|
|
|
from flask_uploads import UploadSet, configure_uploads, IMAGES, \
|
|
|
patch_request_class
|
|
|
from output import out_path, web_path, file_name, direct_path
|
|
|
from program import task
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd() + '\\static\\uploads' # 文件储存地址
|
|
|
|
|
|
photos = UploadSet('photos', IMAGES)
|
|
|
configure_uploads(app, photos)
|
|
|
patch_request_class(app) # 文件大小限制,默认为16MB
|
|
|
|
|
|
# 存储的最开始的图片的名字 后面上传就会改变
|
|
|
photo_name = 'Tsunami_by_hokusai_19th_century.jpg'
|
|
|
app.config['PHOTO_URL'] = os.getcwd() + '\\static\\uploads\\' + 'Tsunami_by_hokusai_19th_century.jpg'
|
|
|
# 现在的图片的版本
|
|
|
app.config['cnt'] = 0
|
|
|
|
|
|
html = '''
|
|
|
<!DOCTYPE html>
|
|
|
<title>Upload File</title>
|
|
|
<h1>图片上传</h1>
|
|
|
<form method=post enctype=multipart/form-data>
|
|
|
<input type=file name=photo>
|
|
|
<input type=submit value=上传>
|
|
|
</form>
|
|
|
'''
|
|
|
|
|
|
pure_html = '''
|
|
|
<!DOCTYPE html>
|
|
|
<title>Processed Image</title>
|
|
|
'''
|
|
|
|
|
|
|
|
|
# 主页面
|
|
|
@app.route('/upload_file', methods=['GET', 'POST'])
|
|
|
def upload_file():
|
|
|
cnt = 0
|
|
|
del_all()
|
|
|
if request.method == 'POST' and 'photo' in request.files:
|
|
|
filename = photos.save(request.files['photo'])
|
|
|
# app.name = filename
|
|
|
app.photo_name = filename
|
|
|
file_url = photos.url(filename)
|
|
|
print(file_url)
|
|
|
app.config['PHOTO_URL'] = os.getcwd() + '\\static\\uploads\\' + filename
|
|
|
print(app.config['PHOTO_URL'] + 'photourl')
|
|
|
img = cv2.imread(app.config['PHOTO_URL']) # 根据路径读取一张图片
|
|
|
cv2.imwrite(out_path(0), img)
|
|
|
print(file_url)
|
|
|
res = {
|
|
|
'code': 0,
|
|
|
'url_before': direct_path(app.config['cnt']) + '?' + str(random.random()),
|
|
|
'url_after': ''
|
|
|
}
|
|
|
print(res)
|
|
|
return jsonify(res)
|
|
|
res = {
|
|
|
'code': -1,
|
|
|
'url_before': '',
|
|
|
'url_after': ''
|
|
|
}
|
|
|
print(res)
|
|
|
return jsonify(res)
|
|
|
|
|
|
|
|
|
@app.route('/upload_style', methods=['GET', 'POST'])
|
|
|
def upload_style():
|
|
|
cnt = 0
|
|
|
del_all()
|
|
|
if request.method == 'POST' and 'photo' in request.files:
|
|
|
filename = photos.save(request.files['photo'])
|
|
|
# app.name = filename
|
|
|
app.photo_name = filename
|
|
|
file_url = photos.url(filename)
|
|
|
print(file_url)
|
|
|
app.config['PHOTO_URL'] = os.getcwd() + '\\static\\uploads\\' + filename
|
|
|
print(app.config['PHOTO_URL'] + 'photourl')
|
|
|
img = cv2.imread(app.config['PHOTO_URL']) # 根据路径读取一张图片
|
|
|
cv2.imwrite(out_path(0), img)
|
|
|
print(file_url)
|
|
|
cv2.imwrite('./img_src/styles/style0.jpg', img)
|
|
|
res = {
|
|
|
'code': 0,
|
|
|
}
|
|
|
print(res)
|
|
|
return jsonify(res)
|
|
|
res = {
|
|
|
'code': -1,
|
|
|
}
|
|
|
print(res)
|
|
|
return jsonify(res)
|
|
|
|
|
|
|
|
|
# 删除uploads文件夹 (不用设置接口)
|
|
|
@app.route('/delete_all')
|
|
|
def del_all():
|
|
|
ls = os.listdir(app.config['UPLOADED_PHOTOS_DEST'])
|
|
|
for i in ls:
|
|
|
c_path = os.path.join(app.config['UPLOADED_PHOTOS_DEST'], i)
|
|
|
if os.path.isdir(c_path):
|
|
|
del_file(c_path)
|
|
|
else:
|
|
|
os.remove(c_path)
|
|
|
app.config['cnt'] = 0
|
|
|
|
|
|
|
|
|
# 用在delete_all里面,不用设置接口 (不用设置接口)
|
|
|
@app.route('/delete_file/<path>')
|
|
|
def del_file(path):
|
|
|
if os.path.exists(path):
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
|
# 通过commmand_id来选择处理的方式,commmand_id具体内容再program.py里面找
|
|
|
@app.route('/deal/<command_id>', methods=['GET', 'POST'])
|
|
|
def deal(command_id):
|
|
|
img = cv2.imread(app.config['PHOTO_URL']) # 根据路径读取一张图片
|
|
|
|
|
|
res = {
|
|
|
'code': -1,
|
|
|
'url_before': '',
|
|
|
'url_after': ''
|
|
|
}
|
|
|
|
|
|
cmd = command_id
|
|
|
if (cmd == 'b' or cmd == 'back'):
|
|
|
# 撤销操作
|
|
|
if (app.config['cnt'] > 1):
|
|
|
if os.path.exists(out_path(app.config['cnt'])):
|
|
|
os.remove(out_path(app.config['cnt']))
|
|
|
print('back success')
|
|
|
print(app.config['cnt'])
|
|
|
app.config['cnt'] = app.config['cnt'] - 1
|
|
|
res = {
|
|
|
'code': 0,
|
|
|
'url_before': direct_path(app.config['cnt'] - 1) + '?' + str(random.random()),
|
|
|
'url_after': direct_path(app.config['cnt']) + '?' + str(random.random())
|
|
|
}
|
|
|
elif (app.config['cnt'] == 1):
|
|
|
res = {
|
|
|
'code': 0,
|
|
|
'url_before': direct_path(app.config['cnt'] - 1) + '?' + str(random.random()),
|
|
|
'url_after': ''
|
|
|
}
|
|
|
app.config['cnt'] = app.config['cnt'] - 1
|
|
|
elif (app.config['cnt'] == 0):
|
|
|
res = {
|
|
|
'code': 1,
|
|
|
'url_before': direct_path(app.config['cnt'] - 1) + '?' + str(random.random()),
|
|
|
'url_after': ''
|
|
|
}
|
|
|
|
|
|
elif (cmd == 's' or cmd == 'save'):
|
|
|
# 保存操作
|
|
|
cnt_max = app.config['cnt']
|
|
|
img = cv2.imread(out_path(app.config['cnt']))
|
|
|
cv2.imwrite(os.getcwd() + '\\static', img)
|
|
|
cnt = 0
|
|
|
cv2.imwrite(out_path(cnt), img)
|
|
|
for id in range(1, cnt_max + 1):
|
|
|
if os.path.exists(out_path(id)):
|
|
|
os.remove(out_path(id))
|
|
|
print('save success')
|
|
|
|
|
|
else:
|
|
|
# 图像处理操作
|
|
|
success = task(cmd, app.config['cnt'])
|
|
|
if (success == 0):
|
|
|
app.config['cnt'] = app.config['cnt'] + 1
|
|
|
res = {
|
|
|
'code': 0,
|
|
|
'url_before': direct_path(app.config['cnt'] - 1) + '?' + str(random.random()),
|
|
|
'url_after': direct_path(app.config['cnt']) + '?' + str(random.random()),
|
|
|
}
|
|
|
str_cnt = str(app.config['cnt'])
|
|
|
print(type(str_cnt))
|
|
|
print(app.config['cnt'])
|
|
|
return jsonify(res)
|
|
|
|
|
|
|
|
|
# 这个download会下载最新的照片
|
|
|
@app.route('/download', methods=['POST', 'GET'])
|
|
|
def download():
|
|
|
direct_path = os.getcwd() + '\\static\\uploads'
|
|
|
# return send_from_directory(direct_path, file_name(app.config['cnt']), as_attachment=True)
|
|
|
response = send_from_directory(direct_path, file_name(app.config['cnt']), as_attachment=True)
|
|
|
response.headers["Access-Control-Expose-Headers"] = "Content-disposition"
|
|
|
return response
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
app.run()
|