LINE Notify 傳送螢幕截圖
這篇文章會使用 Python 的 Requests 和 pyautogui 函式庫,實作一個會自動進行螢幕截圖,並在截圖後將截圖透過 LINE Notify 傳送出去的程式。
注意,LINE Notify 服務已於 2025 年停止,本篇教學僅供參考使用。
執行 pyautogui 會由程式控制電腦滑鼠、鍵盤或畫面,因此所以請使用本機環境 ( 參考:使用 Python 虛擬環境 ) 或使用 Anaconda Jupyter 進行實作 ( 參考:使用 Anaconda ) 。
文章導讀
在實作本篇範例前,請先閱讀「發送 LINE Notify 通知」和「定時自動螢幕截圖」兩篇文章,並預先取得 LINE notify 的權杖。
使用 LINE Notify 傳送螢幕截圖
下方的程式執行後,會使用 pyautogui 函式庫進行截圖並儲存為 test.png,接著使用 requests 函式庫以 POST 的方式,將圖片傳送到指定的 LINE 聊天頻道裡。
注意,LINE Notify 服務已於 2025 年停止,本篇教學僅供參考使用。
import pyautogui
import requests
myScreenshot = pyautogui.screenshot() # 截圖
myScreenshot.save('./test.png') # 儲存為 test.png
url = 'https://notify-api.line.me/api/notify'
token = '你的權杖'
headers = {
'Authorization': 'Bearer ' + token # 設定 LINE Notify 權杖
}
data = {
'message':'測試一下!' # 設定 LINE Notify message ( 不可少 )
}
image = open('./test.png', 'rb') # 以二進位方式開啟圖片
imageFile = {'imageFile' : image} # 設定圖片資訊
data = requests.post(url, headers=headers, data=data, files=imageFile) # 發送 LINE Notify
加入時間資訊、定時截圖
修改上方的程式,加入 time 內建函式庫,將訊息內容更換為截圖當下的時間,使用 sleep 與 for 迴圈搭配,將程式使用「函式」包裝,就能做到定時截圖和發送 LINE Notify 的功能。
import pyautogui
import requests
import time
# 定義截圖的函式
def screenshot():
myScreenshot = pyautogui.screenshot()
myScreenshot.save('./test.png')
t = time.time() # 取得到目前為止的秒數
t1 = time.localtime(t) # 將秒數轉換為 struct_time 格式的時間
now = time.strftime('%Y/%m/%d %H:%M:%S',t1) # 輸出為特定格式的文字
sendLineNotify(now) # 執行發送 LINE Notify 的函式,發送的訊息為時間
# 定義發送 LINE Notify 的函式
def sendLineNotify(msg):
url = 'https://notify-api.line.me/api/notify'
token = '你的權杖'
headers = {
'Authorization': 'Bearer ' + token
}
data = {
'message':msg
}
image = open('./test.png', 'rb')
imageFile = {'imageFile' : image}
data = requests.post(url, headers=headers, data=data, files=imageFile)
# 使用for 迴圈,每隔五秒截圖發送一次
for i in range(5):
screenshot()
time.sleep(5)
小結
這個範例的功能,好像可以應用在「監控小孩子使用電腦的狀況」( 定時發送圖片,看看小孩都在幹嘛 ),但仍然要注意的是,在沒有得到同意的狀況下進行監控,會觸犯刑法第 315 之 1 條呦~
意見回饋
如果有任何建議或問題,可傳送「意見表單」給我,謝謝~