Prompt
How do I find the exact area that changed in a screenshot diff?
Latest observation
Oct 27, 2026
OpenAI APIWeb search: off
To find the exact area that changed in a screenshot diff, the usual approach is to compare the two images pixel-by-pixel and then compute the bounding box around all differing pixels.
Basic idea
- Load both screenshots.
- Ensure they’re the same size.
- Compare each pixel.
- Mark pixels that differ.
- Find the smallest rectangle containing all changed pixels.
Example in Python
Using Pillow and NumPy:
from PIL import Image, ImageChops
import numpy as np
img1 = Image.open("before.png").convert("RGBA")
img2 = Image.open("after.png").convert("RGBA")
if img1.size != img2.size:
raise ValueError("Images must be the same size")
diff = ImageChops.difference(img1, img2)
diff_array = np.array(diff)
# Find pixels that changed
changed = np.any(diff_array != 0, axis=2)
ys, xs = np.where(changed)
if len(xs) == 0:
print("No differences found")
else:
left, right = xs.min(), xs.max()
top, bottom = ys.min(), ys.max()
print(f"Changed area: left={left}, top={top}, right={right}, bottom={bottom}")
If you want the changed region visually highlighted
You can draw the rectangle on top of the image:
from PIL import ImageDraw
draw = ImageDraw.Draw(img2)
draw.rectangle([left, top, right, bottom], outline="red", width=3)
img2.save("changed_area.png")
Notes
- If the screenshots have tiny anti-aliasing or compression differences, you may want to treat pixels as “changed” only if the difference exceeds a threshold.
- If there are multiple separate changed regions, the bounding box gives the overall extent, but not each individual patch. In that case, you’d use connected-component labeling to get each region separately.
If you want, I can also show:
- a JavaScript/browser version,
- how to get multiple changed rectangles instead of one,
- or how to ignore small noise in the diff.