Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import bpy
  2.  
  3. allImages = bpy.data.images
  4.  
  5. imageA = allImages['Image A'] # Original image.
  6. imageB = allImages['Image B'] # Paintover image.
  7.  
  8. if (imageA.size[:] != imageB.size[:]):
  9.     raise Exception("The two images don't have the same size")
  10.    
  11. imageC = allImages.new('Image C', imageA.size[0], imageA.size[1], alpha=True)
  12.  
  13. # Put a copy of the pixels of image A, B and C into Python objects, for speed.
  14. tempPixelsA = imageA.pixels[:] # Tuple.
  15. tempPixelsB = imageB.pixels[:] # Tuple.
  16. tempPixelsC = list(imageC.pixels) # List.
  17.  
  18. channelsPerPixel = 4 # Blender hardcoded value, always 4 channels per pixel (R, G, B, A).
  19. TRANSPARENT_BLACK = (0.0, 0.0, 0.0, 0.0)
  20. anyPixelsChanged = False
  21. for pixelIndex in range(0, len(tempPixelsA), channelsPerPixel):
  22.     pixelB = tempPixelsB[pixelIndex : pixelIndex+channelsPerPixel]
  23.     if tempPixelsA[pixelIndex : pixelIndex+channelsPerPixel] != pixelB:
  24.         tempPixelsC[pixelIndex : pixelIndex+channelsPerPixel] = pixelB
  25.         anyPixelsChanged = True
  26.     else:
  27.         tempPixelsC[pixelIndex : pixelIndex+channelsPerPixel] = TRANSPARENT_BLACK
  28.  
  29. if anyPixelsChanged:
  30.     # Do a "slice assignment" from the 'tempPixelsC' values into the pixels of image C.
  31.     imageC.pixels[:] = tempPixelsC
  32. else:
  33.     allImages.remove(imageC)
  34.     raise Exception('The two images are identical')
  35.  
  36. # End.