Python Forum
Image comparison, a more pythonic method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image comparison, a more pythonic method
#1
I'm comparing two screenshots to determine if a kiosk is frozen.
So both images will have the exactly the same size and structure which makes it an apples to apples comparison.
I'm just wondering if there is a faster method of comparison, or if there's an interesting pythonic method of achieving this goal.
import pyautogui
from PIL import Image
from time import sleep

pyautogui.screenshot('current_screen.png')
img_a_pixels = Image.open('current_screen.png').getdata()
#sleep(60) #Used in practice
pyautogui.screenshot('current_screen.png')
img_b_pixels = Image.open('current_screen.png').getdata()

difference = 0
for pixel_a, pixel_b in zip(img_a_pixels, img_b_pixels):
    if pixel_a != pixel_b:
        difference += 1
print(difference)
This returns an int telling how many pixels are different, an allowance of a few hundred has to be allotted for the clock, mouse, and blinking cursor.
Reply


Messages In This Thread
Image comparison, a more pythonic method - by Grok_It - Jun-06-2018, 04:19 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020