获取两个图像之间的增量更新

Get incremental update between two images

场景:每隔 10 秒我想截图并上传到服务器。当然,为了提高效率,我应该只取最后一张截图的不同之处,而不是整个屏幕。我认为 python 中可能有一种简单的方法可以做到这一点,但我没有找到任何解决方案。

当然,我找到了很多这样的答案: CV - Extract differences between two images

但该解决方案并没有真正帮助我,因为我不想要两个图像的差异,我只想要更新的像素及其最新值。

有人知道解决这个问题的好方法吗?这是我目前所拥有的:

def save_screen(path):
    import pyautogui
    pyautogui.screenshot().convert('L').save(path)

def get_incremental(image, image2):
    # ImageChops.difference from PIL is not sufficient
    # must I use numpy or something?
    pass

def upload_incremental(image):
    # upload to server using requests
    pass

def main(path):
    import time
    save_screen(path + '1')
    time.sleep(10)
    save_screen(path + '2')
    from PIL import Image
    image1 = Image.open(path + 1)
    image2 = Image.open(path + 2)
    upload_incremental(get_incremental(image1, image2))

你认为我必须使用 numpy 来获取图像之间的增量更新吗? Algorithms Python - Difference Between Two Images

还有其他想法吗?谢谢!

这不是理想的解决方案,但现在已经足够了。获得最大的变化内部矩形和 return 那:

import numpy as np
from PIL import Image, ImageChops

def crop_to_largest_changed_area(
    before: Image.Image,
    after: Image.Image
) -> 'tuple((left, top), Image.Image)':

    def black_out_clock(arr, yx, mode='L'):
        ''' assumes clock is at bottom right '''
        black = 0 if mode in ('L', '1') else (0, 0, 0)  # assume RGB
        arr[yx[0]-50:yx[0], yx[1]-120:yx[1]] = black
        return arr

    arr = np.array(ImageChops.difference(before, after))
    bbox = Image.fromarray(
        black_out_clock(
            arr=arr,
            yx=arr.shape,
            mode=after.mode)).getbbox()
    return (bbox[0:2], after.crop(bbox))

你也可以取而代之的是从数组中改变的单个像素,但是你必须自己压缩它,我不想处理所有这些,我宁愿获取比我需要的更多并利用其上的 png 压缩。