Skip to content

Instantly share code, notes, and snippets.

@RobiMez
Last active September 27, 2023 20:30
Show Gist options
  • Save RobiMez/0c9970545df6bf7f1e88cc47a07958d8 to your computer and use it in GitHub Desktop.
Save RobiMez/0c9970545df6bf7f1e88cc47a07958d8 to your computer and use it in GitHub Desktop.
Take a screenshot of your display every n seconds and remove duplicate with similarity checking ( run with admin mode )
from datetime import datetime
import time , os
import pyautogui
import pygetwindow as gw
from skimage import io, metrics
import os
# Define a function to compare images using SSIM
def are_images_similar(image_path1, image_path2):
image1 = io.imread(image_path1)
image2 = io.imread(image_path2)
print(type(image1))
print(type(image2))
print(image2[0])
# Get image dimensions
height1, width1, _ = image1.shape
height2, width2, _ = image2.shape
if height1 != height2 or width1 != width2:
print("Image dimensions are not the same.")
return False
# Calculate SSIM
ssim_score = metrics.structural_similarity(image1, image2, full=True, channel_axis=2)[0]
print(f"Structural similarity: {ssim_score}")
return ssim_score >= 0.997
interval = 1
save_directory = "./screenshots/"
prev_path = ""
while True:
try:
os.system('cls')
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
screenshot_path = os.path.join(save_directory, f"sc_{timestamp}.png")
print(f'Taking screenshot with {screenshot_path}')
screenshot = pyautogui.screenshot()
screenshot.save(screenshot_path)
active = gw.getActiveWindowTitle()
open_windows = gw.getAllWindows()
print(f"\n\nReport for {timestamp}")
if prev_path :
# Check if the screenshots are similar
if are_images_similar(prev_path, screenshot_path):
print("Similar screenshots Removing a duplicate")
os.remove(screenshot_path)
else:
print("Different screenshots , Keeping both ... ")
prev_path = screenshot_path
else :
prev_path = screenshot_path
time.sleep(interval)
except KeyboardInterrupt:
print("Screenshot script stopped.")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment