1
VGMaps Social Board / Re: Are there any graphics apps that can remove backgrounds or compare pixels?
« on: February 03, 2025, 08:38:29 am »
Here's a python function that will do it, given two input images and an output path (black for matching areas, red for different):
Code: [Select]
from PIL import Image
import numpy as np
def compare_images(image_path1, image_path2, output_path):
img1 = Image.open(image_path1)
img2 = Image.open(image_path2)
if img1.size != img2.size:
raise ValueError("Images must be the same size.")
# Convert images to numpy arrays
arr1 = np.array(img1)
arr2 = np.array(img2)
if arr1.shape != arr2.shape:
raise ValueError("Images must have the same number of channels.")
# Find where pixels are the same (set to black) and different (set to red)
diff = np.zeros_like(arr1)
mask = np.any(arr1 != arr2, axis=-1) # True where pixels differ
diff[..., 0] = mask * 255 # Red channel
diff[..., 1] = 0 # Green channel
diff[..., 2] = 0 # Blue channel
# Convert back to image
output_img = Image.fromarray(diff)
output_img.save(output_path)