Spaces:
Sleeping
Sleeping
Elvis Claude Opus 4.5 commited on
Commit ·
f0b0450
1
Parent(s): abd1771
Fix tone curve to show actual tonal distribution
Browse filesUse percentile analysis instead of CDF to generate meaningful tone curves:
- p2/p98 for black/white points (lifted shadows, crushed highlights)
- p10/p25/p50/p75/p90 for shadow/midtone/highlight distribution
- Curves now vary significantly between different images
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
529 unmodified lines530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581529 unmodified lines"""Analyze the image's tonal distribution and return control points for a tone curve.Returns a list of [x, y] points where x is input (0-100) and y is output (0-100)."""# Compute histogramhist, _ = np.histogram(gray.flatten(), bins=256, range=(0, 256))# Compute cumulative distribution function (CDF)cdf = hist.cumsum()cdf_normalized = cdf / cdf[-1] # Normalize to 0-1# Sample the CDF at key points to create curve control points# We sample at shadows, quarter-tones, midtones, three-quarter-tones, highlightssample_points = [0, 32, 64, 96, 128, 160, 192, 224, 255]curve_points = []for input_val in sample_points:# The CDF tells us what percentage of pixels are at or below this value# We use this to derive the "output" value for the curveoutput_val = cdf_normalized[input_val] * 100input_normalized = (input_val / 255) * 100curve_points.append([round(input_normalized, 1), round(output_val, 1)])# Analyze characteristics for additional curve shaping# Check if shadows are lifted (black point is raised)black_point = np.percentile(gray, 2)white_point = np.percentile(gray, 98)# Adjust curve based on actual tonal rangeif black_point > 20: # Lifted shadows# Raise the shadow portion of the curvefor i, pt in enumerate(curve_points):if pt[0] < 25:lift_amount = (black_point / 255) * 100 * 0.5curve_points[i][1] = min(100, pt[1] + lift_amount * (1 - pt[0]/25))if white_point < 235: # Crushed highlights# Lower the highlight portionfor i, pt in enumerate(curve_points):if pt[0] > 75:crush_amount = ((255 - white_point) / 255) * 100 * 0.5curve_points[i][1] = max(0, pt[1] - crush_amount * ((pt[0] - 75) / 25))# Ensure curve is valid (monotonically increasing, within bounds)for i, pt in enumerate(curve_points):curve_points[i][1] = max(0, min(100, pt[1]))# Convert to list of floats for JSON serializationreturn [[float(p[0]), float(p[1])] for p in curve_points]def _generate_summary(self, camera: dict, lighting: dict, color: dict) -> str:return (529 unmodified lines530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595529 unmodified lines"""Analyze the image's tonal distribution and return control points for a tone curve.Returns a list of [x, y] points where x is input (0-100) and y is output (0-100).The curve represents how tones are distributed in the image compared to a linear response."""# Get key percentile values to understand tonal distributionp0 = float(np.percentile(gray, 0)) # Absolute blackp2 = float(np.percentile(gray, 2)) # Near black (shadow point)p10 = float(np.percentile(gray, 10)) # Deep shadowsp25 = float(np.percentile(gray, 25)) # Quarter tonesp50 = float(np.percentile(gray, 50)) # Midtones (median)p75 = float(np.percentile(gray, 75)) # Three-quarter tonesp90 = float(np.percentile(gray, 90)) # Highlightsp98 = float(np.percentile(gray, 98)) # Near white (highlight point)p100 = float(np.percentile(gray, 100)) # Absolute white# Normalize percentiles to 0-100 scaledef normalize(val):return (val / 255.0) * 100.0# Build curve points: [input, output]# Input is the "expected" linear position, output is where that tone actually fallscurve_points = []# Black point - where do the darkest 2% of pixels fall?# If shadows are lifted, this won't be at 0black_out = normalize(p2)curve_points.append([0.0, max(0, black_out - 2)]) # Slight offset for visual# Shadow regionshadow_out = normalize(p10)# Compare to linear expectation (10% of tones should be at ~25.5 brightness linearly)curve_points.append([10.0, shadow_out])# Quarter tonesquarter_out = normalize(p25)curve_points.append([25.0, quarter_out])# Midtones - this is key for contrastmid_out = normalize(p50)curve_points.append([50.0, mid_out])# Three-quarter tonesthreequarter_out = normalize(p75)curve_points.append([75.0, threequarter_out])# Highlight regionhighlight_out = normalize(p90)curve_points.append([90.0, highlight_out])# White point - where do the brightest 2% of pixels fall?white_out = normalize(p98)curve_points.append([100.0, min(100, white_out + 2)]) # Slight offset for visual# Ensure monotonically increasing and within boundsfor i in range(len(curve_points)):curve_points[i][1] = max(0, min(100, curve_points[i][1]))if i > 0:# Ensure each point is at least as high as the previouscurve_points[i][1] = max(curve_points[i][1], curve_points[i-1][1])return [[round(p[0], 1), round(p[1], 1)] for p in curve_points]def _generate_summary(self, camera: dict, lighting: dict, color: dict) -> str:return (
backend/app/services/analyzer.py+56-42