Spaces:
Running
Running
Commit ·
d77d844
1
Parent(s): 20baeac
Fix matplotlib colormap API for matplotlib 3.9+ compatibility
Browse files- tampering.py +9 -2
tampering.py
CHANGED
|
@@ -247,13 +247,20 @@ def annotate_before_after(orig_img, tamper_meta, box_width=4):
|
|
| 247 |
|
| 248 |
|
| 249 |
def overlay_heatmap_on_image(base_img, heat_2d, alpha=0.55, cmap="hot"):
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
base = base_img.convert("RGBA")
|
| 252 |
W, H = base.size
|
| 253 |
h = heat_2d.astype(np.float32)
|
| 254 |
if h.max() > 0: h = (h - h.min()) / (h.max() - h.min() + 1e-9)
|
| 255 |
h_resized = cv2.resize(h, (W, H), interpolation=cv2.INTER_CUBIC)
|
| 256 |
-
rgba = (
|
| 257 |
rgba[..., 3] = (h_resized * 255 * alpha).astype(np.uint8)
|
| 258 |
overlay = Image.fromarray(rgba, mode="RGBA")
|
| 259 |
return Image.alpha_composite(base, overlay).convert("RGB")
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
def overlay_heatmap_on_image(base_img, heat_2d, alpha=0.55, cmap="hot"):
|
| 250 |
+
# Use the modern colormap registry (matplotlib >= 3.5),
|
| 251 |
+
# fall back to the deprecated cm.get_cmap on older versions.
|
| 252 |
+
try:
|
| 253 |
+
import matplotlib as mpl
|
| 254 |
+
cmap_fn = mpl.colormaps[cmap]
|
| 255 |
+
except (AttributeError, KeyError):
|
| 256 |
+
import matplotlib.cm as cm
|
| 257 |
+
cmap_fn = cm.get_cmap(cmap)
|
| 258 |
base = base_img.convert("RGBA")
|
| 259 |
W, H = base.size
|
| 260 |
h = heat_2d.astype(np.float32)
|
| 261 |
if h.max() > 0: h = (h - h.min()) / (h.max() - h.min() + 1e-9)
|
| 262 |
h_resized = cv2.resize(h, (W, H), interpolation=cv2.INTER_CUBIC)
|
| 263 |
+
rgba = (cmap_fn(h_resized) * 255).astype(np.uint8)
|
| 264 |
rgba[..., 3] = (h_resized * 255 * alpha).astype(np.uint8)
|
| 265 |
overlay = Image.fromarray(rgba, mode="RGBA")
|
| 266 |
return Image.alpha_composite(base, overlay).convert("RGB")
|