import bpy
allImages = bpy.data.images
imageA = allImages['Image A'] # Original image.
imageB = allImages['Image B'] # Paintover image.
if (imageA.size[:] != imageB.size[:]):
raise Exception("The two images don't have the same size")
imageC = allImages.new('Image C', imageA.size[0], imageA.size[1], alpha=True)
# Put a copy of the pixels of image A, B and C into Python objects, for speed.
tempPixelsA = imageA.pixels[:] # Tuple.
tempPixelsB = imageB.pixels[:] # Tuple.
tempPixelsC = list(imageC.pixels) # List.
channelsPerPixel = 4 # Blender hardcoded value, always 4 channels per pixel (R, G, B, A).
TRANSPARENT_BLACK = (0.0, 0.0, 0.0, 0.0)
anyPixelsChanged = False
for pixelIndex in range(0, len(tempPixelsA), channelsPerPixel):
pixelB = tempPixelsB[pixelIndex : pixelIndex+channelsPerPixel]
if tempPixelsA[pixelIndex : pixelIndex+channelsPerPixel] != pixelB:
tempPixelsC[pixelIndex : pixelIndex+channelsPerPixel] = pixelB
anyPixelsChanged = True
else:
tempPixelsC[pixelIndex : pixelIndex+channelsPerPixel] = TRANSPARENT_BLACK
if anyPixelsChanged:
# Do a "slice assignment" from the 'tempPixelsC' values into the pixels of image C.
imageC.pixels[:] = tempPixelsC
else:
allImages.remove(imageC)
raise Exception('The two images are identical')
# End.