SHARE
TWEET

Pillow - substract images to find differences

furas Jul 30th, 2018 (edited) 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PIL import Image, ImageChops
  2. import numpy as np
  3.  
  4. FOLDER = '/home/username/'
  5. img1 = Image.open(FOLDER + 'image1.png').convert('RGB') # without A (RGBA) to remove transparency
  6. img2 = Image.open(FOLDER + 'image2.png').convert('RGB') # without A (RGBA) to remove transparency
  7.  
  8. diff = ImageChops.difference(img2, img1).convert('L') # L = grayscale (it will covert three numbers R,G,B into one number)
  9. diff.save(FOLDER + 'diff.png')
  10.  
  11. arr = np.asarray(diff) # convert to numpy's array
  12. data = np.nonzero(arr) # find nonzero values
  13.  
  14. for x, y  in zip(data[0], data[1]):
  15.     print(x, y)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top