And here it is. Python to accept a base64 return from the API, and flag it if it has the artifacts damaging the output.
“sharpened‑edge” artifact detector
Key ideas
| Signal | Why it helps | Scale in score |
|---|---|---|
| Sharpness / Tenengrad | Oversharpened swirl images have 5‑20× the high‑frequency energy of a normal photo. | 0 – 0.45 |
| Edge‑pixel ratio | Fault frames are almost all edges. | 0 – 0.25 |
| Inverse saturation | They are nearly monochrome. | 0 – 0.12 |
| Extreme gray share | Huge portion of pixels are ≈ 0 or 255. | 0 – 0.10 |
| Inverse colourfulness | Natural scenes are far more colourful. | 0 – 0.08 |
On the images from this forum thread
good_house score=0.366 artifact=False
bad_house score=0.893 artifact=True
good_family score=0.175 artifact=False
bad_family score=0.891 artifact=True
good_biophage score=0.226 artifact=False
bad_biophage score=0.763 artifact=True
Damaged pictures are confidently caught; clean photographs pass. 0.70 seems a good threshold, unless you are making pictures of a thousand zebras.
Feel free to tweak the weightings that are internal if you want to log or return thresholds also, against more samples – but this configuration should already work robustly on the characteristic high‑contrast, swirly edge failure mode. You can give at least one or two automated retries, relying on the model’s lack of temperature or seed control to do something different.
A readable function, for what was developed as a class, by GPT-4.5
Here’s a clearly refactored, human-readable version of the function, with meaningful variable names and explanatory comments to help developers understand or modify it:
import base64
from io import BytesIO
from PIL import Image
import numpy as np
def detect_swirly_edge_artifact(base64_image: str, threshold: float = 0.70) -> bool:
"""
Detects the presence of a high-contrast, swirly-edge artifact in an image.
Args:
base64_image (str):
The image encoded in base64. Supports PNG, JPG, WebP.
A data-URL header is permitted and will be stripped automatically.
threshold (float, optional):
The detection threshold value between 0 and 1.
Higher thresholds decrease false positives but might miss subtle artifacts.
Default is 0.70.
Returns:
bool:
True if the artifact is detected, False otherwise.
"""
# ----- Step 1: Convert base64 string to PIL image -----
if ';base64,' in base64_image:
base64_image = base64_image.split(';base64,')[-1]
image_bytes = base64.b64decode(base64_image)
image = Image.open(BytesIO(image_bytes)).convert("RGB")
# ----- Step 2: Convert image to NumPy array (RGB channels) -----
img_array = np.asarray(image, dtype=np.float32)
# ----- Step 3: Calculate luminance -----
luminance = 0.299 * img_array[..., 0] + \
0.587 * img_array[..., 1] + \
0.114 * img_array[..., 2]
# ----- Step 4: Calculate edge density -----
# Measure the gradient magnitude of luminance to identify edges
gradient_x = np.abs(np.diff(luminance, axis=1, prepend=luminance[:, :1]))
gradient_y = np.abs(np.diff(luminance, axis=0, prepend=luminance[:1, :]))
gradient_magnitude = np.hypot(gradient_x, gradient_y)
# Define a pixel as 'edge' if gradient magnitude exceeds this threshold
edge_detection_threshold = 60.0
edge_pixel_ratio = np.mean(gradient_magnitude > edge_detection_threshold)
# Normalize edge density to 0-1 (typical artifacts have very high edge density)
normalized_edge_ratio = min(edge_pixel_ratio / 0.40, 1.0)
# ----- Step 5: Calculate sharpness using Tenengrad method -----
# Tenengrad: a common autofocus measure of high-frequency detail
gx = np.zeros_like(luminance)
gy = np.zeros_like(luminance)
gx[1:-1, 1:-1] = (luminance[1:-1, 2:] - luminance[1:-1, :-2]) * 2 + \
(luminance[:-2, 2:] - luminance[:-2, :-2]) + \
(luminance[2:, 2:] - luminance[2:, :-2])
gy[1:-1, 1:-1] = (luminance[2:, 1:-1] - luminance[:-2, 1:-1]) * 2 + \
(luminance[2:, 2:] - luminance[:-2, 2:]) + \
(luminance[2:, :-2] - luminance[:-2, :-2])
# Tenengrad sharpness metric (mean squared gradient)
tenengrad_sharpness = np.mean(gx ** 2 + gy ** 2)
# Normalizing sharpness (artifact images have excessively high sharpness)
normalized_sharpness = min(tenengrad_sharpness / 150_000.0, 1.0)
# ----- Step 6: Calculate mean inverse saturation (artifact images have low saturation) -----
img_normalized = img_array / 255.0
max_rgb = np.max(img_normalized, axis=-1)
min_rgb = np.min(img_normalized, axis=-1)
# Avoid division by zero for black pixels
with np.errstate(divide='ignore', invalid='ignore'):
saturation = np.where(max_rgb == 0, 0, (max_rgb - min_rgb) / max_rgb)
inverse_mean_saturation = 1.0 - np.mean(saturation)
# ----- Step 7: Calculate proportion of extreme grayscale pixels -----
# (pixels nearly black or nearly white indicate the artifact)
near_black_threshold = 30
near_white_threshold = 225
extreme_pixels = np.logical_or(
luminance < near_black_threshold,
luminance > near_white_threshold
)
extreme_pixel_ratio = np.mean(extreme_pixels)
normalized_extreme_ratio = min(extreme_pixel_ratio / 0.50, 1.0)
# ----- Step 8: Calculate inverse colorfulness (artifacts lack colors) -----
red, green, blue = img_array[..., 0], img_array[..., 1], img_array[..., 2]
rg_diff = np.abs(red - green)
yb_diff = np.abs(0.5 * (red + green) - blue)
# Colorfulness metric based on variance and mean differences between color channels
colorfulness = np.sqrt(np.var(rg_diff) + np.var(yb_diff)) + \
0.3 * np.sqrt(np.mean(rg_diff) ** 2 + np.mean(yb_diff) ** 2)
# Normalize inverse colorfulness (artifacts have lower colorfulness)
inverse_colorfulness = 1.0 - min(colorfulness / 40.0, 1.0)
# ----- Step 9: Combine features into composite anomaly score -----
anomaly_score = (
0.25 * normalized_edge_ratio + # Edge density
0.45 * normalized_sharpness + # Sharpness
0.12 * inverse_mean_saturation + # Low saturation
0.10 * normalized_extreme_ratio + # Extreme brightness/darkness
0.08 * inverse_colorfulness # Low colorfulness
)
# ----- Step 10: Flag image if anomaly_score exceeds threshold -----
return anomaly_score >= threshold
You might also have the function return the anomaly_score instead of just a boolean flag.
How to use the refactored function:
# Example usage, use a file instead of your API return base64:
with open('path_to_image.png', 'rb') as file:
base64_str = base64.b64encode(file.read()).decode('ascii')
# Detect artifact presence with function taking base64 image files
if detect_swirly_edge_artifact(base64_str, threshold=0.70):
print("Artifact detected in image.")
else:
print("No artifact detected in image.")
Developer Notes:
-
Adjustable Threshold:
- The default threshold is
0.70. - Lower threshold values increase sensitivity but may introduce false positives.
- Higher threshold values improve precision, reducing false positives.
- The default threshold is
-
Modifiable Parameters:
- Internal parameters (e.g.,
edge_detection_threshold,near_black_threshold,near_white_threshold, and normalization values) are easily adjustable to refine detection based on your specific dataset.
- Internal parameters (e.g.,
-
External Dependencies:
- Relies only on widely-used libraries (
numpy,Pillow).
- Relies only on widely-used libraries (
Advanced code idea to automate
Log into your SMTP email server, send support@openai every bad image request id and a MIME file attachment, and ask for an org ID credit until they fix it.




