You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义图像XOR计算函数解析及与Pillow内置logical_xor的差异咨询

Hey there! Let's break down this custom "XOR" function step by step, then compare it to Pillow's built-in ImageChops.logical_xor to clear up the differences.

Custom get_xor Function Execution Logic

First, let's look at the code again:

from PIL import Image, ImageChops
def get_xor(image_1, image_2):
    i1 = ImageChops.invert(image_1)
    i2 = ImageChops.invert(image_2)
    return ImageChops.invert(ImageChops.add(ImageChops.subtract(i2, i1), ImageChops.subtract(i1, i2)))

Let's unpack each operation (we'll use 8-bit grayscale pixels as an example—RGB channels work the same way per-channel):

  1. Invert both input images:
    • ImageChops.invert() takes each pixel value v and converts it to 255 - v. So i1 is the inverse of image_1, i2 is the inverse of image_2.
  2. Two saturated subtraction operations:
    • ImageChops.subtract(i2, i1) calculates i2 - i1, but uses saturated arithmetic: if the result is negative, it clamps to 0. This simplifies to max(a - b, 0) (where a is a pixel from image_1, b from image_2).
    • ImageChops.subtract(i1, i2) does the reverse: max(b - a, 0).
  3. Add the two subtraction results:
    • Adding these two values gives us max(a - b, 0) + max(b - a, 0) = |a - b| (the absolute difference between the original pixel values). This also uses saturated addition (clamps to 255 if the sum exceeds the maximum pixel value).
  4. Invert the final sum:
    • The final invert converts |a - b| to 255 - |a - b|.

What does this actually compute?

For binary images (pixels are only 0 or 255), this results in the XNOR (not XOR) operation:

  • If a and b are the same (0&0 or 255&255), the result is 255 (bright)
  • If a and b are different (0&255), the result is 0 (dark)

For grayscale images, it's a continuous version of this: pixels that are very similar have bright values, while pixels with large differences have dark values. This is not a true bitwise XOR—it's an arithmetic operation focused on pixel value similarity.

Differences from Pillow's ImageChops.logical_xor

The built-in logical_xor is a strict bitwise operation, and it behaves very differently from the custom function:

  • Core operation:
    • logical_xor: Compares each binary bit of the two pixel values. For each bit, if the bits are different, it sets the result bit to 1; if they're the same, it sets it to 0. The final pixel value is the combination of these bits.
    • Custom get_xor: Uses arithmetic operations (invert, subtract, add) to compute the inverted absolute difference of pixel values—no bitwise comparison involved.
  • Visual effect:
    • logical_xor: Highlights differences between pixels. The more bits that differ between two pixel values, the brighter the result. For binary images, this gives true XOR (same pixels = dark, different = bright).
    • Custom get_xor: Highlights similarities. The closer two pixel values are, the brighter the result; larger differences result in darker pixels.
  • Use cases:
    • logical_xor: Ideal for tasks requiring strict logical bitwise operations, like creating image masks, detecting exact bit-level differences, or working with binary image logic.
    • Custom get_xor: Useful for visualizing pixel similarity (e.g., in image matching or alignment tasks where you want to emphasize regions that match).

内容的提问来源于stack exchange,提问作者Amanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:12:47