Spaces:
Runtime error
Runtime error
advanced curve added
Browse files- advanced.json +29 -0
- film_simulation.py +67 -10
advanced.json
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Kodak Portra 400": {
|
3 |
+
"base_color": [255, 248, 240],
|
4 |
+
"color_curves": {
|
5 |
+
"R": {
|
6 |
+
"x": [0, 0.25, 0.5, 0.75, 1],
|
7 |
+
"y": [0, 0.27, 0.55, 0.80, 1]
|
8 |
+
},
|
9 |
+
"G": {
|
10 |
+
"x": [0, 0.25, 0.5, 0.75, 1],
|
11 |
+
"y": [0, 0.26, 0.52, 0.78, 1]
|
12 |
+
},
|
13 |
+
"B": {
|
14 |
+
"x": [0, 0.25, 0.5, 0.75, 1],
|
15 |
+
"y": [0, 0.24, 0.50, 0.75, 1]
|
16 |
+
}
|
17 |
+
},
|
18 |
+
"contrast": 1.05,
|
19 |
+
"saturation": 0.95,
|
20 |
+
"chromatic_aberration": 0.1,
|
21 |
+
"blur": 0.05,
|
22 |
+
"advanced_curve": {
|
23 |
+
"hue_values": [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330],
|
24 |
+
"saturation_multipliers": [1.2, 1.1, 1.3, 1.0, 0.9, 1.1, 1.0, 1.2, 1.5, 1.4, 1.3, 1.1],
|
25 |
+
"hue_shifts": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
26 |
+
"value_multipliers": [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
|
27 |
+
}
|
28 |
+
}
|
29 |
+
}
|
film_simulation.py
CHANGED
@@ -11,7 +11,7 @@ import multiprocessing
|
|
11 |
from functools import partial
|
12 |
|
13 |
class FilmProfile:
|
14 |
-
def __init__(self, name, color_curves, contrast, saturation, chromatic_aberration, blur, base_color, grain_amount, grain_size):
|
15 |
self.name = name
|
16 |
self.color_curves = color_curves
|
17 |
self.contrast = contrast
|
@@ -21,6 +21,7 @@ class FilmProfile:
|
|
21 |
self.base_color = base_color
|
22 |
self.grain_amount = grain_amount
|
23 |
self.grain_size = grain_size
|
|
|
24 |
|
25 |
def create_curve(curve_data):
|
26 |
x = np.array(curve_data['x'])
|
@@ -37,6 +38,7 @@ def load_film_profiles_from_json(json_path):
|
|
37 |
channel: create_curve(curve_data)
|
38 |
for channel, curve_data in data['color_curves'].items()
|
39 |
}
|
|
|
40 |
profiles[name] = FilmProfile(
|
41 |
name,
|
42 |
color_curves=color_curves,
|
@@ -46,7 +48,8 @@ def load_film_profiles_from_json(json_path):
|
|
46 |
blur=data.get('blur', 0),
|
47 |
base_color=tuple(data.get('base_color', (255, 255, 255))),
|
48 |
grain_amount=data.get('grain_amount', 0),
|
49 |
-
grain_size=data.get('grain_size', 1)
|
|
|
50 |
)
|
51 |
return profiles
|
52 |
|
@@ -56,6 +59,52 @@ def apply_color_curves(image, curves):
|
|
56 |
result[:,:,i] = curves[channel](image[:,:,i])
|
57 |
return result
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def apply_chromatic_aberration_pil(img, strength):
|
60 |
width, height = img.size
|
61 |
center_x, center_y = width // 2, height // 2
|
@@ -113,12 +162,20 @@ def cross_process(image):
|
|
113 |
|
114 |
return image
|
115 |
|
116 |
-
def apply_film_profile(img, profile, chroma_override=None, blur_override=None, color_temp=6500, cross_process_flag=False):
|
117 |
img_array = np.array(img).astype(np.float32) / 255.0
|
118 |
|
119 |
img_linear = colour.models.eotf_sRGB(img_array)
|
120 |
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
img_srgb = colour.models.eotf_inverse_sRGB(img_color_adjusted)
|
124 |
|
@@ -150,7 +207,7 @@ def apply_film_profile(img, profile, chroma_override=None, blur_override=None, c
|
|
150 |
return img_saturated
|
151 |
|
152 |
def process_image(args):
|
153 |
-
image_path, profile, chroma_override, blur_override, color_temp, cross_process_flag, output_filename = args
|
154 |
with Image.open(image_path) as img:
|
155 |
exif_data = img.getexif()
|
156 |
|
@@ -158,7 +215,7 @@ def process_image(args):
|
|
158 |
if orientation in [3, 6, 8]:
|
159 |
img = img.rotate({3: 180, 6: 270, 8: 90}[orientation], expand=True)
|
160 |
|
161 |
-
processed_image = apply_film_profile(img, profile, chroma_override, blur_override, color_temp, cross_process_flag)
|
162 |
|
163 |
if exif_data:
|
164 |
exif_data[274] = 1
|
@@ -175,13 +232,12 @@ def get_optimal_pool_size(target_memory_usage=75):
|
|
175 |
available_memory = 100 - get_memory_usage()
|
176 |
cpu_count = multiprocessing.cpu_count()
|
177 |
|
178 |
-
# Start with all CPUs and reduce until we're under the target memory usage
|
179 |
for i in range(cpu_count, 0, -1):
|
180 |
estimated_memory_usage = get_memory_usage() + (available_memory / cpu_count) * i
|
181 |
if estimated_memory_usage <= target_memory_usage:
|
182 |
return i
|
183 |
|
184 |
-
return 1
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
parser = argparse.ArgumentParser(description="Apply film profiles to an image.")
|
@@ -191,6 +247,7 @@ if __name__ == "__main__":
|
|
191 |
parser.add_argument("--blur", type=float, help="Override blur amount")
|
192 |
parser.add_argument("--color_temp", type=int, default=6500, help="Color temperature (default: 6500K)")
|
193 |
parser.add_argument("--cross_process", action="store_true", help="Apply cross-processing effect")
|
|
|
194 |
parser.add_argument("--parallel", action="store_true", help="Enable parallel processing")
|
195 |
args = parser.parse_args()
|
196 |
|
@@ -205,7 +262,7 @@ if __name__ == "__main__":
|
|
205 |
process_args = []
|
206 |
for profile_name, profile in film_profiles.items():
|
207 |
output_filename = f"{input_path_base}_{profile_name.replace(' ', '_')}.jpg"
|
208 |
-
process_args.append((args.input_image, profile, args.chroma, args.blur, args.color_temp, args.cross_process, output_filename))
|
209 |
|
210 |
if args.parallel:
|
211 |
pool_size = get_optimal_pool_size()
|
@@ -217,4 +274,4 @@ if __name__ == "__main__":
|
|
217 |
for arg in process_args:
|
218 |
process_image(arg)
|
219 |
|
220 |
-
print("All images processed.")
|
|
|
11 |
from functools import partial
|
12 |
|
13 |
class FilmProfile:
|
14 |
+
def __init__(self, name, color_curves, contrast, saturation, chromatic_aberration, blur, base_color, grain_amount, grain_size, advanced_curve=None):
|
15 |
self.name = name
|
16 |
self.color_curves = color_curves
|
17 |
self.contrast = contrast
|
|
|
21 |
self.base_color = base_color
|
22 |
self.grain_amount = grain_amount
|
23 |
self.grain_size = grain_size
|
24 |
+
self.advanced_curve = advanced_curve
|
25 |
|
26 |
def create_curve(curve_data):
|
27 |
x = np.array(curve_data['x'])
|
|
|
38 |
channel: create_curve(curve_data)
|
39 |
for channel, curve_data in data['color_curves'].items()
|
40 |
}
|
41 |
+
advanced_curve = data.get('advanced_curve', None)
|
42 |
profiles[name] = FilmProfile(
|
43 |
name,
|
44 |
color_curves=color_curves,
|
|
|
48 |
blur=data.get('blur', 0),
|
49 |
base_color=tuple(data.get('base_color', (255, 255, 255))),
|
50 |
grain_amount=data.get('grain_amount', 0),
|
51 |
+
grain_size=data.get('grain_size', 1),
|
52 |
+
advanced_curve=advanced_curve
|
53 |
)
|
54 |
return profiles
|
55 |
|
|
|
59 |
result[:,:,i] = curves[channel](image[:,:,i])
|
60 |
return result
|
61 |
|
62 |
+
def interpolate_circular(x, y, new_x):
|
63 |
+
# Interpolate considering the circular nature of hue values (0-360 degrees)
|
64 |
+
x_extended = np.concatenate((x, x + 360))
|
65 |
+
y_extended = np.concatenate((y, y))
|
66 |
+
interp_func = interp1d(x_extended, y_extended, kind='cubic')
|
67 |
+
return interp_func(new_x % 360)
|
68 |
+
|
69 |
+
def apply_advanced_curve(image, advanced_curve):
|
70 |
+
hsv_image = colour.RGB_to_HSV(image)
|
71 |
+
hue_values = np.array(advanced_curve['hue_values'])
|
72 |
+
saturation_multipliers = np.array(advanced_curve['saturation_multipliers'])
|
73 |
+
hue_shifts = np.array(advanced_curve['hue_shifts'])
|
74 |
+
value_multipliers = np.array(advanced_curve['value_multipliers'])
|
75 |
+
|
76 |
+
hue = hsv_image[:,:,0] * 360 # Convert to degrees
|
77 |
+
saturation = hsv_image[:,:,1]
|
78 |
+
value = hsv_image[:,:,2]
|
79 |
+
|
80 |
+
# Interpolate saturation multipliers
|
81 |
+
interp_saturation_multipliers = interpolate_circular(hue_values, saturation_multipliers, hue)
|
82 |
+
|
83 |
+
# Apply saturation multipliers with a curve to prevent blowout
|
84 |
+
max_saturation = 1.0
|
85 |
+
interp_saturation_multipliers = np.clip(interp_saturation_multipliers, 0, max_saturation / saturation)
|
86 |
+
|
87 |
+
saturation *= interp_saturation_multipliers
|
88 |
+
|
89 |
+
# Interpolate hue shifts and apply them
|
90 |
+
interp_hue_shifts = interpolate_circular(hue_values, hue_shifts, hue)
|
91 |
+
hue = (hue + interp_hue_shifts) % 360
|
92 |
+
|
93 |
+
# Interpolate value multipliers and apply them
|
94 |
+
interp_value_multipliers = interpolate_circular(hue_values, value_multipliers, hue)
|
95 |
+
|
96 |
+
# Apply value multipliers with a curve to prevent blowout
|
97 |
+
max_value = 1.0
|
98 |
+
interp_value_multipliers = np.clip(interp_value_multipliers, 0, max_value / value)
|
99 |
+
|
100 |
+
value *= interp_value_multipliers
|
101 |
+
|
102 |
+
hsv_image[:,:,0] = hue / 360 # Convert back to [0, 1] range
|
103 |
+
hsv_image[:,:,1] = saturation
|
104 |
+
hsv_image[:,:,2] = value
|
105 |
+
|
106 |
+
return colour.HSV_to_RGB(hsv_image)
|
107 |
+
|
108 |
def apply_chromatic_aberration_pil(img, strength):
|
109 |
width, height = img.size
|
110 |
center_x, center_y = width // 2, height // 2
|
|
|
162 |
|
163 |
return image
|
164 |
|
165 |
+
def apply_film_profile(img, profile, chroma_override=None, blur_override=None, color_temp=6500, cross_process_flag=False, curve_type="auto"):
|
166 |
img_array = np.array(img).astype(np.float32) / 255.0
|
167 |
|
168 |
img_linear = colour.models.eotf_sRGB(img_array)
|
169 |
|
170 |
+
if curve_type == "advanced" or (curve_type == "auto" and profile.advanced_curve):
|
171 |
+
img_color_adjusted = apply_advanced_curve(img_linear, profile.advanced_curve)
|
172 |
+
elif curve_type == "color" or (curve_type == "auto" and not profile.advanced_curve):
|
173 |
+
img_color_adjusted = apply_color_curves(img_linear, profile.color_curves)
|
174 |
+
elif curve_type == "both":
|
175 |
+
if profile.color_curves:
|
176 |
+
img_color_adjusted = apply_color_curves(img_linear, profile.color_curves)
|
177 |
+
if profile.advanced_curve:
|
178 |
+
img_color_adjusted = apply_advanced_curve(img_color_adjusted, profile.advanced_curve)
|
179 |
|
180 |
img_srgb = colour.models.eotf_inverse_sRGB(img_color_adjusted)
|
181 |
|
|
|
207 |
return img_saturated
|
208 |
|
209 |
def process_image(args):
|
210 |
+
image_path, profile, chroma_override, blur_override, color_temp, cross_process_flag, curve_type, output_filename = args
|
211 |
with Image.open(image_path) as img:
|
212 |
exif_data = img.getexif()
|
213 |
|
|
|
215 |
if orientation in [3, 6, 8]:
|
216 |
img = img.rotate({3: 180, 6: 270, 8: 90}[orientation], expand=True)
|
217 |
|
218 |
+
processed_image = apply_film_profile(img, profile, chroma_override, blur_override, color_temp, cross_process_flag, curve_type)
|
219 |
|
220 |
if exif_data:
|
221 |
exif_data[274] = 1
|
|
|
232 |
available_memory = 100 - get_memory_usage()
|
233 |
cpu_count = multiprocessing.cpu_count()
|
234 |
|
|
|
235 |
for i in range(cpu_count, 0, -1):
|
236 |
estimated_memory_usage = get_memory_usage() + (available_memory / cpu_count) * i
|
237 |
if estimated_memory_usage <= target_memory_usage:
|
238 |
return i
|
239 |
|
240 |
+
return 1
|
241 |
|
242 |
if __name__ == "__main__":
|
243 |
parser = argparse.ArgumentParser(description="Apply film profiles to an image.")
|
|
|
247 |
parser.add_argument("--blur", type=float, help="Override blur amount")
|
248 |
parser.add_argument("--color_temp", type=int, default=6500, help="Color temperature (default: 6500K)")
|
249 |
parser.add_argument("--cross_process", action="store_true", help="Apply cross-processing effect")
|
250 |
+
parser.add_argument("--curve", choices=["color", "advanced", "both"], default="auto", help="Type of curve to apply (color, advanced, both)")
|
251 |
parser.add_argument("--parallel", action="store_true", help="Enable parallel processing")
|
252 |
args = parser.parse_args()
|
253 |
|
|
|
262 |
process_args = []
|
263 |
for profile_name, profile in film_profiles.items():
|
264 |
output_filename = f"{input_path_base}_{profile_name.replace(' ', '_')}.jpg"
|
265 |
+
process_args.append((args.input_image, profile, args.chroma, args.blur, args.color_temp, args.cross_process, args.curve, output_filename))
|
266 |
|
267 |
if args.parallel:
|
268 |
pool_size = get_optimal_pool_size()
|
|
|
274 |
for arg in process_args:
|
275 |
process_image(arg)
|
276 |
|
277 |
+
print("All images processed.")
|