Author Topic: Are there any graphics apps that can remove backgrounds or compare pixels?  (Read 2386 times)

0 Members and 3 Guests are viewing this topic.

Offline JonLeung

  • Administrator
  • *****
  • Posts: 3721
Hey, folks, does anyone know of any applications, or functions in existing ones like GIMP or Paint Shop Pro or Photoshop, that can easily handle these scenarios?

---

1. Background removal


Let's say I have something like the two screenshots above: one with foreground elements as well as the background, and another with only the background.
Can I subtract any pixels that match, essentially removing the background, to get an output of only the unique pixels from the first screenshot?

I know there are background removal tools online as well as apps that can do that, though those are usually suited for photos, and usually higher-res.
I don't believe they would work well with low-res pixel art, like screenshots, especially when background colours might be as vibrant as foreground elements.

Keep in mind that this is an example, I know that for Super NES emulation, and a few other platforms, it is possible to disable various background and/or sprite layers.
What I'm thinking of could be handy for isolating sprites in computer games (like '90s point-and-click adventure games), or if I only have existing screenshots on hand.

---

2. Pixel comparison


Another function that could also work if we're comparing pixels could be just simply identifying pixels that are the same or different across screenshots of the same size.
I'd like to take two screenshots, and get an output like that third image there, simply a two-colour image, with green showing the alike pixels and red showing the differing pixels.

Or, whatever colour.  It could be black and white, doesn't matter.

This would make it easy to identify differences in similar images (like if I need to check versions of a pixel art image in progress).
I'm sure image masks could be made of that to isolate things (such as sprites, though that'd be similar to the earlier example).

I just had a thought... one could take a screenshot of a point-and-click adventure game before any items are picked up, then pick up all the items on screen and take another screenshot, and the resulting output image would be silhouettes of all the items.
Select just the red, float it, expand it by one pixel, paste the original output so that the expansion becomes an outline, stick that on the original screenshot, and boom, we've outlined all the items!  Or all the changeable/interactable elements.
Could certainly be handy for stuff like that.

---

So, does anyone know of any applications or easy methods to do the functions I described above?  Please and thanks in advance for any help.

Offline G.E.R.

  • Jr. Member
  • **
  • Posts: 87
Re: Are there any graphics apps that can remove backgrounds or compare pixels?
« Reply #1 on: February 02, 2025, 02:57:01 am »
For pixel comparison it's very simple:
You should to make one layer on negative (Image - Adjustments - Invert), set transparent 50% (Opacity 50% on layer-menu) and place this layer on top of the first layer. The same pixels will be gray, the difference pixels will be another colors. Another way: just set blend mode Difference on top layer.


For background removal: I never used it, but you can merge previous layers and select background, move selection to the first image and copy area on new layer.

You can see all of this in this example

Offline VGCartography

  • Jr. Member
  • **
  • Posts: 50
Re: Are there any graphics apps that can remove backgrounds or compare pixels?
« Reply #2 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)