File size: 7,993 Bytes
83e35a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
#!/usr/bin/env python3
"""
Comic Editor Server - Interactive bubble editing
"""
from flask import Flask, render_template, request, jsonify, send_from_directory
import json
import os
from pathlib import Path
app = Flask(__name__)
# Paths
COMIC_DATA_PATH = 'output/comic_data.json'
FRAMES_DIR = 'frames/final'
@app.route('/')
def index():
"""Redirect to editor"""
return render_template('comic_editor.html')
@app.route('/editor')
def editor():
"""Comic editor page"""
return render_template('comic_editor.html')
@app.route('/load_comic')
def load_comic():
"""Load existing comic data"""
try:
# Check if we have saved data
if os.path.exists(COMIC_DATA_PATH):
with open(COMIC_DATA_PATH, 'r') as f:
data = json.load(f)
return jsonify(data)
else:
# Generate from existing comic
data = generate_comic_data()
return jsonify(data)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/save_comic', methods=['POST'])
def save_comic():
"""Save comic data"""
try:
data = request.json
# Create output directory
os.makedirs('output', exist_ok=True)
# Save JSON data
with open(COMIC_DATA_PATH, 'w') as f:
json.dump(data, f, indent=2)
# Generate static HTML
generate_static_html(data)
return jsonify({'success': True, 'message': 'Comic saved successfully!'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/frames/<path:filename>')
def serve_frame(filename):
"""Serve frame images"""
return send_from_directory('frames/final', filename)
@app.route('/export_comic')
def export_comic():
"""Export comic as static HTML"""
try:
if os.path.exists(COMIC_DATA_PATH):
with open(COMIC_DATA_PATH, 'r') as f:
data = json.load(f)
html = generate_static_html(data)
return html, 200, {'Content-Type': 'text/html'}
else:
return "No comic data found", 404
except Exception as e:
return str(e), 500
def generate_comic_data():
"""Generate comic data from existing frames and subtitles"""
# Get all frames
frames = sorted([f for f in os.listdir(FRAMES_DIR) if f.endswith('.png')])
# Load subtitles if available
subtitles = []
if os.path.exists('test1.srt'):
import srt
with open('test1.srt', 'r') as f:
subtitles = list(srt.parse(f.read()))
# Create comic layout
page_width = 800
page_height = 1080
panel_width = 380
panel_height = 280
padding = 10
pages = []
current_page = {
'width': page_width,
'height': page_height,
'panels': [],
'bubbles': []
}
# 2x2 grid layout
positions = [
(padding, padding),
(page_width - panel_width - padding, padding),
(padding, padding + panel_height + 20),
(page_width - panel_width - padding, padding + panel_height + 20)
]
for i, frame in enumerate(frames[:16]): # Max 16 frames
panel_index = i % 4
# Add panel
x, y = positions[panel_index]
current_page['panels'].append({
'x': x,
'y': y,
'width': panel_width,
'height': panel_height,
'image': f'/frames/{frame}'
})
# Add bubble with subtitle text
if i < len(subtitles):
text = subtitles[i].content.strip()
else:
text = f"Panel {i+1}"
current_page['bubbles'].append({
'id': f'bubble_{i}',
'x': x + 20,
'y': y + 20,
'width': 150,
'height': 60,
'text': text,
'panelIndex': panel_index
})
# Start new page after 4 panels
if panel_index == 3 and i < len(frames) - 1:
pages.append(current_page)
current_page = {
'width': page_width,
'height': page_height,
'panels': [],
'bubbles': []
}
# Add last page
if current_page['panels']:
pages.append(current_page)
return {'pages': pages}
def generate_static_html(data):
"""Generate static HTML from comic data"""
html = """
<!DOCTYPE html>
<html>
<head>
<title>My Comic</title>
<style>
body {
margin: 0;
padding: 20px;
background: #f0f0f0;
font-family: Arial, sans-serif;
}
.comic-page {
position: relative;
background: white;
margin: 20px auto;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.comic-panel {
position: absolute;
border: 2px solid #333;
overflow: hidden;
}
.comic-panel img {
width: 100%;
height: 100%;
object-fit: contain;
background: #000;
}
.speech-bubble {
position: absolute;
background: white;
border: 3px solid #333;
border-radius: 20px;
padding: 15px;
font-family: 'Comic Sans MS', cursive;
font-size: 14px;
font-weight: bold;
text-align: center;
z-index: 10;
}
.bubble-tail {
position: absolute;
bottom: -15px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 5px solid transparent;
border-top: 20px solid #333;
transform: rotate(-20deg);
}
.bubble-tail::after {
content: '';
position: absolute;
bottom: 3px;
left: -12px;
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 4px solid transparent;
border-top: 16px solid white;
}
</style>
</head>
<body>
"""
for page_idx, page in enumerate(data['pages']):
html += f'<div class="comic-page" style="width:{page["width"]}px;height:{page["height"]}px;">\n'
# Add panels
for panel in page['panels']:
html += f'''<div class="comic-panel" style="left:{panel["x"]}px;top:{panel["y"]}px;width:{panel["width"]}px;height:{panel["height"]}px;">
<img src="{panel["image"]}">
</div>\n'''
# Add bubbles
for bubble in page['bubbles']:
html += f'''<div class="speech-bubble" style="left:{bubble["x"]}px;top:{bubble["y"]}px;width:{bubble["width"]}px;height:{bubble["height"]}px;">
{bubble["text"]}
<div class="bubble-tail"></div>
</div>\n'''
html += '</div>\n'
html += """
</body>
</html>
"""
# Save to file
with open('output/comic_static.html', 'w') as f:
f.write(html)
return html
# Integration with existing app
def add_editor_routes(existing_app):
"""Add editor routes to existing Flask app"""
existing_app.route('/editor')(editor)
existing_app.route('/load_comic')(load_comic)
existing_app.route('/save_comic', methods=['POST'])(save_comic)
existing_app.route('/export_comic')(export_comic)
# Also update static JS to handle API calls
@existing_app.route('/api/load_comic')
def api_load_comic():
"""API endpoint for loading comic data"""
return load_comic()
print("✅ Comic editor routes added!")
if __name__ == '__main__':
print("🎨 Starting Comic Editor Server...")
print("📝 Visit http://localhost:5001/editor to edit your comic")
app.run(debug=True, port=5001) |