-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
214 lines (188 loc) · 6.34 KB
/
main.py
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
import numpy as np
from flask import Flask, request, send_file, render_template_string
import json
import io
app = Flask(__name__)
def convert_json_to_lut(json_data):
"""Convert JSON data to LUT format."""
try:
# LUT size (33x33x33 is common)
lut_size = 33
def apply_color_balance(r, g, b, balance):
"""Apply color balance adjustment."""
return [
r * balance[0],
g * balance[1],
b * balance[1]
]
# Generating LUT
lut = np.zeros((lut_size, lut_size, lut_size, 3))
# Fill the LUT
for r in range(lut_size):
for g in range(lut_size):
for b in range(lut_size):
r_val = r / (lut_size - 1)
g_val = g / (lut_size - 1)
b_val = b / (lut_size - 1)
r_adj, g_adj, b_adj = apply_color_balance(
r_val, g_val, b_val,
json_data["data"]["s"]["colorBalance"]
)
lut[r, g, b] = [r_adj, g_adj, b_adj]
# Create .cube file content
output = io.StringIO()
output.write(f"TITLE \"{json_data['name']}\"\n")
output.write(f"LUT_3D_SIZE {lut_size}\n")
for r in range(lut_size):
for g in range(lut_size):
for b in range(lut_size):
r_val, g_val, b_val = lut[r, g, b]
output.write(f"{r_val:.6f} {g_val:.6f} {b_val:.6f}\n")
return output.getvalue(), json_data['name']
except Exception as e:
raise ValueError(f"Error processing JSON: {str(e)}")
# HTML template with modern styling
HTML_TEMPLATE = '''
<!DOCTYPE html>
<html>
<head>
<title>JSON to LUT Converter</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
}
.upload-section {
margin: 20px 0;
text-align: center;
}
.button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin: 10px 0;
}
.button:hover {
background-color: #45a049;
}
.error {
color: #ff0000;
margin: 10px 0;
padding: 10px;
border: 1px solid #ff0000;
border-radius: 4px;
background-color: #fff2f2;
}
.instructions {
margin: 20px 0;
padding: 15px;
background-color: #f8f9fa;
border-radius: 4px;
border-left: 4px solid #4CAF50;
}
#file-name {
margin-top: 10px;
font-style: italic;
color: #666;
}
</style>
</head>
<body>
<div class="container">
<h1>JSON to LUT Converter</h1>
<div class="instructions">
<h3>How to use:</h3>
<ol>
<li>Click "Choose File" to select your JSON file</li>
<li>Click "Convert to LUT" to process the file</li>
<li>Download your .cube file when ready</li>
</ol>
</div>
<div class="upload-section">
<form action="/convert" method="post" enctype="multipart/form-data">
<input type="file" name="json_file" accept=".json"
onchange="document.getElementById('file-name').textContent = this.files[0].name">
<div id="file-name"></div>
<input type="submit" value="Convert to LUT" class="button">
</form>
{% if error %}
<div class="error">
{{ error }}
</div>
{% endif %}
{% if success %}
<form action="/download" method="post">
<input type="hidden" name="lut_content" value="{{ lut_content }}">
<input type="hidden" name="filename" value="{{ filename }}">
<input type="submit" value="Download .cube File" class="button">
</form>
{% endif %}
</div>
</div>
</body>
</html>
'''
@app.route('/')
def home():
return render_template_string(HTML_TEMPLATE)
@app.route('/convert', methods=['POST'])
def convert():
try:
if 'json_file' not in request.files:
return render_template_string(HTML_TEMPLATE, error="No file uploaded")
file = request.files['json_file']
if file.filename == '':
return render_template_string(HTML_TEMPLATE, error="No file selected")
if not file.filename.endswith('.json'):
return render_template_string(HTML_TEMPLATE, error="Please upload a JSON file")
# Read and parse JSON
json_content = file.read().decode('utf-8')
json_data = json.loads(json_content)
# Convert to LUT
lut_content, filename = convert_json_to_lut(json_data)
return render_template_string(
HTML_TEMPLATE,
success=True,
lut_content=lut_content,
filename=filename
)
except json.JSONDecodeError:
return render_template_string(HTML_TEMPLATE, error="Invalid JSON file")
except ValueError as e:
return render_template_string(HTML_TEMPLATE, error=str(e))
except Exception as e:
return render_template_string(HTML_TEMPLATE, error=f"An error occurred: {str(e)}")
@app.route('/download', methods=['POST'])
def download():
lut_content = request.form['lut_content']
filename = request.form['filename']
# Create BytesIO object
bio = io.BytesIO()
bio.write(lut_content.encode('utf-8'))
bio.seek(0)
return send_file(
bio,
mimetype='text/plain',
as_attachment=True,
download_name=f"{filename.replace(' ', '_')}.cube"
)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)