jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

Is there a faster way to judge whether two pictures are different?

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?

Upvotes: 1

Views: 399

Answers (3)

Piotr Rarus
Piotr Rarus

Reputation: 942

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

Upvotes: 1

a_guest
a_guest

Reputation: 36239

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')

Upvotes: 0

Masklinn
Masklinn

Reputation: 42197

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).

Upvotes: 0

Related Questions