1

I am using it to tell whether now screenshot is different from last screenshot. Now I use

with open('last_screenshot.bmp','rb+') as f:
    org = f.read()
with open('now_screenshot.bmp','rb+') as f:
    new = f.read()
if(org==new):
    print("The pictures are same")

Is there a faster way to do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • 1
    You want to know if they are exactly the same? Does this answer your question?: [link](https://stackoverflow.com/q/23195522/11261546) – Ivan Jan 17 '20 at 13:41
  • 4
    Nope, i think that's the best option! – marcos Jan 17 '20 at 13:49
  • @nathancy This is simalar to ```PIL.ImageChops.difference```.But what I want to do is to know whether they are different instead of where is different between them.Thanks for response. – jizhihaoSAMA Jan 18 '20 at 04:18
  • @jizhihaoSAMA yes SSIM returns a difference mask, so you can check the mask to determine if they are different. If there are white pixels on the resulting mask, then the two images are different. SSIM is a possible method which can help you to determine IF they are different and where they are different – nathancy Jan 20 '20 at 20:52
  • There is a chance that [template matching](https://docs.opencv.org/master/d4/dc6/tutorial_py_template_matching.html) might be marginally faster, but ` == ` seems like it should be good. – bfris Jul 02 '20 at 23:59

3 Answers3

1

You won't get nowhere comparing pixels. Your options:

  1. extract features using descriptor (HOG, SIFT, SURF, ORB), match them, see how many were matched; example
  2. calculate hashes, compute hamming metric here's example
  3. take pretrained embedder; when it comes to images it's pretty easy, just take activation of penultimate layer; it can be inception, vgg etc
Piotr Rarus
  • 884
  • 8
  • 16
0

Well you could iterate the files chunk by chunk instead of reading the entire thing in memory.

Alternatively, use filecmp or shell out to cmp(1).

Masklinn
  • 34,759
  • 3
  • 38
  • 57
0

You can use filecmp.cmp(..., shallow=False) which ships with the standard library. This won't read the whole files into memory but instead read them in chunks and short-circuit when a differing chunk is encountered. Example usage:

import filecmp

if filecmp('last_screenshot.bmp', 'now_screenshot.bmp', shallow=False):
    print('Files compare equal')
a_guest
  • 34,165
  • 12
  • 64
  • 118