72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
"""
|
|
图片编辑器 - Image Editor v1.2.7
|
|
前端图片处理工具:合并、分割、挖孔填充、圆形切图、文字图片等
|
|
v1.2.7: 修复合并图片紧密拼接问题
|
|
|
|
端口: 19018
|
|
"""
|
|
|
|
from flask import Flask, render_template, jsonify, request, send_file
|
|
from flask_cors import CORS
|
|
from pathlib import Path
|
|
import base64
|
|
from datetime import datetime
|
|
import uuid
|
|
|
|
app = Flask(__name__, static_folder='static', static_url_path='/static')
|
|
CORS(app)
|
|
|
|
OUTPUT_DIR = Path(__file__).parent / 'outputs'
|
|
OUTPUT_DIR.mkdir(exist_ok=True)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/api/health')
|
|
def health():
|
|
return jsonify({'status': 'ok', 'version': '1.2.7', 'time': datetime.now().isoformat()})
|
|
|
|
@app.route('/api/save', methods=['POST'])
|
|
def save_image():
|
|
data = request.get_json()
|
|
image_data = data.get('image', '')
|
|
filename = data.get('filename', f'image_{uuid.uuid4().hex[:8]}.png')
|
|
|
|
if not image_data:
|
|
return jsonify({'error': '无图片数据'}), 400
|
|
|
|
if image_data.startswith('data:image'):
|
|
image_data = image_data.split(',', 1)[1]
|
|
|
|
filepath = OUTPUT_DIR / filename
|
|
filepath.write_bytes(base64.b64decode(image_data))
|
|
|
|
return jsonify({'success': True, 'filename': filename})
|
|
|
|
@app.route('/api/download/<filename>')
|
|
def download_image(filename):
|
|
filepath = OUTPUT_DIR / filename
|
|
if not filepath.exists():
|
|
return jsonify({'error': '文件不存在'}), 404
|
|
return send_file(filepath, as_attachment=True)
|
|
|
|
@app.route('/api/list')
|
|
def list_images():
|
|
images = []
|
|
for f in OUTPUT_DIR.glob('*.png'):
|
|
images.append({
|
|
'filename': f.name,
|
|
'size': f.stat().st_size,
|
|
'time': datetime.fromtimestamp(f.stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S')
|
|
})
|
|
return jsonify({'images': sorted(images, key=lambda x: x['time'], reverse=True)})
|
|
|
|
if __name__ == '__main__':
|
|
print("=" * 50)
|
|
print("图片编辑器 - Image Editor v1.2.7")
|
|
print("=" * 50)
|
|
print("修复合并图片紧密拼接问题")
|
|
print(f"访问地址: http://localhost:19018")
|
|
print("=" * 50)
|
|
app.run(host='0.0.0.0', port=19018, debug=True) |