python - 我如何使用 pil 在 python 中查看两个图像之间的区别?
问题描述
我正在尝试制作一个从我的 macbook 的相机(或任何相机)看的程序,并让我知道发生了什么事情(灯打开/关闭、任何动作等),该程序每 1 秒截取一次屏幕截图并比较最后一张照片是用刚刚拍的那张。
这是我的while循环中的代码
while True:
time.sleep(1)
image = capture_image()
if last_image == None:
last_image = image
# compare the two images
print('image', image)
print('last image', last_image)
print('')
last_image = image
解决方案
这是一个了解两张图片之间差异的示例。也许它可以解决您的问题。
from PIL import Image
from PIL import ImageChops
img1 = Image.open(yourPath)
img2 = Image.open(yourPath)
# make sure img1,img2 have the same picture width and height.
diff = ImageChops.difference(img1, img2)
diff.show()
在您的情况下,这可能有效。
from PIL import ImageChops
While True:
time.sleep(1)
.......
diff = ImageChops.difference(Now_Image,Last_Image)
If diff.getbbox() is None:
print("Now_Image and Last_Image are same.")
# diff.show()
# or you can handle the diff picture.
图像将显示这些diff
图片之间的差异。两张图片之间的差异会显示给您,并且相同的组件将是黑色的。
推荐阅读
- sql - 什么时候应该为 SQL 中的子查询添加 TAB?
- powershell - Powershell 时间跨度总和,结果大于 24 小时
- python - Spyder 中的 TensorFlow 2.3.0 (Python 3.7)
- python - 在列表上的加法和减法之间交替
- flutter - Flutter url_launcher 未处理的异常:无法启动 youtube url(由 canLaunch 引起)
- python-3.x - to_pandas() 得到了一个意外的关键字参数
- python - 如何创建一个计时器,计时在python中按下某个键的时间?
- javascript - 将原始图像数据转换为 html 中可用的数据
- django-models - 我在 django 的动态 urls 系统中有问题
- html - HTML + CSS:由于标题固定,按钮无法点击