python实现实时电脑监控


利用python,实现定时的对屏幕截图,然后它的用途呢, 可以实时的进行电脑监控。
如果能够连接上服务器,或者添加上邮件功能,就可以实现实时的监控了.

第一版代码:
仅仅实现对屏幕的监控,每隔1s,保存电脑截屏到指定目录

import time
import schedule
from PIL import ImageGrab
import imageio

count = time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime())
counter = count.split('-')[-1]
tmp_counter = 0

def screen():
	im =ImageGrab.grab()
	im.save("E:\pic/"+str(count[:-2])+str(tmp_counter)+'.png','PNG')

	
schedule.every(1).seconds.do(screen)
while True:
	schedule.run_pending()		
	time.sleep(1)

第二版代码,添加新功能,将每一分钟的截图组合成gif格式的图片

import time
import schedule
from PIL import ImageGrab
import imageio

count = time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime())
counter = count.split('-')[-1]
tmp_counter = 0

def screen():
	im =ImageGrab.grab()
	im.save("E:\pic/"+str(count[:-2])+str(tmp_counter)+'.png','PNG')
	
schedule.every(1).seconds.do(screen)
while True:
	schedule.run_pending()
	print(time.ctime())
	count = time.strftime("%Y-%m-%d_%H-%M-%S",time.localtime())
	counter = count.split('-')[-1]
	print ("counter  is  %s " % counter)
	tmp_counter =tmp_counter+1
	
	if int(counter) == 59:
		print("start to make GIF ")	
		
		imagelist=[]
		frames=[]
		gif_name="E:\pic/"+count[:-3]+".gif"
		for i in range(1,tmp_counter):
			imagename="E:\pic/"+str(count[:-2])+str(i)+'.png'
			print ("The image [%s]  append to image list" % imagename)
			imagelist.append(imagename)
		print (imagelist)
		for image_ in imagelist:
				frames.append(imageio.imread(image_))
		print("start to make GIF %s" % gif_name)	
		imageio.mimsave(gif_name,frames,'GIF',duration=0.1)
		imagelist.clear()
		frames.clear()
		tmp_counter=0
			
	time.sleep(1)

猜你喜欢

转载自blog.csdn.net/qiqiyingse/article/details/81668345
今日推荐