基于计算机视觉的梦幻西游辅助脚本(只用于开发学习技术)
一個基于計算機視覺開發的夢幻西游輔助腳本
(為了不讓別有用心的人用軟件牟利,雖然軟件打包好了,但僅供有興趣的人學習代碼,不提供軟件下載)
從有想法到弄出來花了兩三天,很多知識都是現學的,哈哈,雖然有點累,但是蠻開心的。
【注】本工具不為盈利,只為興趣,學以致用而已。
麻煩覺得不錯的人點個star哦!
【注1】腳本或程序需要跑在管理員權限運行才行。因為鼠標點擊需要管理員權限。只截圖不需要。
【注2】本項目代碼完整,存放在我的github上面,文中只是展示了一部分代碼。
項目下載地址
【注3】類似下面的資源圖片不會提供哦。
我的代碼力都有截圖保存的部分被我注釋了,想實踐的同學可以根據自己的窗口的情況先在目標區域截圖保存作為基準,然后以后每次運行都截圖來和保存的圖比較即可。
本教程分四部分:
(1)使用python 的pywin32這個庫來實現對Windows窗口的操作和點擊。
(2)使用PIL來實現圖像相似度的計算。
(3)使用tkinter這個庫來實現圖形化界面。
(4)使用pyinstaller這個庫來實現把源程序打包成.exe文件。
一、pywin32的主要作用
1.捕獲窗口;
def resolution(): # 獲取屏幕分辨率return win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)# 獲取夢幻西游窗口信息,返回一個矩形窗口 def get_window_info():wdname = u'《夢幻西游》手游'handle = win32gui.FindWindow(0, wdname) # 獲取窗口句柄if handle == 0:return Noneelse:return win32gui.GetWindowRect(handle)2.模擬鼠標鍵盤動作;
def move_click(x, y, t=0): # 移動鼠標并點擊左鍵win32api.SetCursorPos((x, y)) # 設置鼠標位置(x, y)win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN |win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) # 點擊鼠標左鍵if t == 0:time.sleep(random.random()*2+1) # sleep一下else:time.sleep(t)return 0二、使用PIL來抓取圖像并實現相似度計算
#師門任務 def shimen(window_size):global is_startis_start = Truetopx, topy = window_size[0], window_size[1]# 使用按鈕(比如是x1,y1,x2,y2)shiyong = Image.open("shiyong_jpg")shiyong_hash = get_hash(shiyong)# shiyong.show()# print(shiyong_hash)# 購買寵物(比如是x1,y1,x2,y2)goumai_cw = Image.open("goumai_cw_jpg")goumai_cw_hash = get_hash(goumai_cw)# 上交藥品按鈕(比如是x1,y1,x2,y2)shangjiao_yp = Image.open("shangjiao_yp_jpg")shangjiao_yp_hash = get_hash(shangjiao_yp)# 上交寵物按鈕shangjiao_cw = Image.open("shangjiao_cw_jpg")shangjiao_cw_hash = get_hash(shangjiao_cw)#師門任務欄shimen = Image.open("shimen_jpg")shimen_hash = get_hash(shimen)# 師門任務欄shimen_songxin = Image.open("shimen_songxin_jpg")shimen_songxin_hash = get_hash(shimen_songxin)# 藥店購買goumai_yp = Image.open("goumai_yp_jpg")goumai_yp_hash = get_hash(goumai_yp)# 商城購買goumai_sc = Image.open("goumai_sc_jpg")goumai_sc_hash = get_hash(goumai_sc)i=0count = 0while is_start:time.sleep(2)i=i+1print("第%i次循環" %i)# 使用按鈕(635 ,510 ,710 ,540)img_shiyong = ImageGrab.grab((topx + get_posx(635, window_size), topy + get_posy(510, window_size), topx + get_posx(710, window_size), topy + get_posy(540, window_size)))# img_shiyong.show()# img_shiyong.save("shiyong.jpg")# print("顯示圖片")# print(get_hash(img_shiyong))# print(hamming(get_hash(img_shiyong), shiyong_hash, 20))if hamming(get_hash(img_shiyong), shiyong_hash, 20):move_click(topx + get_posx(670, window_size), topy + get_posy(525, window_size))print("點擊使用")time.sleep(3)continue# 購買寵物610,505,740,540img_goumai_cw = ImageGrab.grab((topx + get_posx(610, window_size), topy + get_posy(505, window_size), topx + get_posx(740, window_size), topy + get_posy(540, window_size)))# img_goumai_cw.save("goumai_cw.jpg")if hamming(get_hash(img_goumai_cw), goumai_cw_hash, 20):move_click(topx +get_posx(680, window_size), topy + get_posy(520, window_size))print("點擊購買寵物")time.sleep(3)continue# 藥店購買 570,470,700,505img_goumai_yp = ImageGrab.grab((topx + get_posx(560, window_size), topy + get_posy(460, window_size), topx + get_posx(690, window_size), topy + get_posy(500, window_size)))# img_goumai_yp.save("goumai_yp.jpg")if hamming(get_hash(img_goumai_yp), goumai_yp_hash, 20):move_click(topx + get_posx(620, window_size), topy + get_posy(480, window_size))print("點擊購買藥")time.sleep(3)continue# 商城購買 520,520,715,550img_goumai_sc = ImageGrab.grab((topx + get_posx(580, window_size), topy + get_posy(515, window_size), topx + get_posx(715, window_size), topy + get_posy(550, window_size)))# img_goumai_sc.save("goumai_sc.jpg")if hamming(get_hash(img_goumai_sc), goumai_sc_hash, 20):move_click(topx + get_posx(650, window_size), topy + get_posy(530, window_size))print("點擊商城購買")time.sleep(3)continue# 上交藥品按鈕 600,470,700,500img_shangjiao_yp = ImageGrab.grab((topx + get_posx(590, window_size), topy + get_posy(465, window_size), topx + get_posx(695, window_size), topy + get_posy(490, window_size)))# img_shangjiao_yp .save("shangjiao_yp.jpg")if hamming(get_hash(img_shangjiao_yp), shangjiao_yp_hash, 20):move_click(topx + get_posx(640, window_size), topy + get_posy(478, window_size))print("點擊藥品上交")time.sleep(3)continue# 上交寵物按鈕img_shangjiao_cw = ImageGrab.grab((topx + get_posx(500, window_size), topy + get_posy(460, window_size), topx + get_posx(600, window_size), topy + get_posy(500, window_size)))# img_shangjiao_cw.save("shangjiao_cw.jpg")if hamming(get_hash(img_shangjiao_cw), shangjiao_cw_hash, 20):move_click(topx + get_posx(550, window_size), topy + get_posy(480, window_size))print("點擊寵物上交")time.sleep(3)continue# 師門任務按鈕img_shimen_songxin = ImageGrab.grab((topx + get_posx(620, window_size), topy + get_posy(345, window_size), topx + get_posx(700, window_size), topy + get_posy(370, window_size)))# img_shimen_songxin .save("shimen_songxin1.jpg")if hamming(get_hash(img_shimen_songxin), shimen_songxin_hash, 30):move_click(topx + get_posx(660, window_size), topy + get_posy(360, window_size))print("點擊師門任務")time.sleep(3)continue# # 師門任務欄 630,150,665,175img_shimen = ImageGrab.grab((topx + get_posx(630, window_size), topy + get_posy(160, window_size), topx + get_posx(670, window_size), topy + get_posy(180, window_size)))# img_shimen.save("shimen.jpg")if hamming(get_hash(img_shimen), shimen_hash, 40):move_click(topx + get_posx(650, window_size), topy + get_posy(170, window_size))print("點擊師門任務欄")time.sleep(3)continue具體思路就是抓取對應位置的圖像和已經保存的圖像進行相似度計算,(這里使用的是計算漢明距離,值越小說明越相似。)如果出現相似的情況就說明事件發生,然后點擊按鈕。否則什么也不做。這樣就避免了傳統的定時定點點擊的弊端。我們的腳本可以每次都點擊,也可以很多次不點。經測試,效果很好。就和人一樣只有在需要點擊的時候才會點擊。平時什么也不做。
三、tkinter圖像化界面
# 啟動 if __name__ == "__main__":screen_resolution = resolution()# print(screen_resolution)window_size = get_window_info()print(window_size)global is_start# 創建主窗口root = tk.Tk()root.title("夢幻西游手游輔助")root.minsize(300, 250)root.maxsize(300, 250)# 創建按鈕button_shimen = tk.Button(root, text=u"師門", command=lambda: MyThread(shimen, window_size), width = 15,height = 2)button_shimen.place(relx=0.2, rely=0.15, width=200)button_shimen.pack()button_bangpai = tk.Button(root, text="幫派", command=lambda: MyThread(bang_pai, window_size), width = 15,height = 2)button_bangpai.place(relx=0.2, rely=0.35, width=200)button_bangpai.pack()button_baotu = tk.Button(root, text="寶圖", command=lambda: MyThread(baotu,window_size), width = 15,height = 2)button_baotu.place(relx=0.4, rely=0.55, width=200)button_baotu.pack()button_zhuagui = tk.Button(root, text="帶隊抓鬼", command=lambda: MyThread(zhua_gui, window_size), width = 15,height = 2)button_zhuagui.place(relx=0.4, rely=0.65, width=100)button_zhuagui.pack()button_tingzhi = tk.Button(root,text=u"停止", command=lambda: MyThread(stop), width = 15,height = 2)button_tingzhi.place(relx=0.4, rely=0.85, width=200)button_tingzhi.pack()root.mainloop()結果展示:
四、pyinstaller 打包
直接使用pyinstaller打包的話,如圖片之類的資源文件并不會被打包。所以把圖片需要轉化成.py文件。在需要的地方導入才能使用。(當然也有其他方法,我使用的是這個)
【注】這段代碼來源于CSDN
#!/usr/bin/env python # _*_ coding:utf-8 _*_import base64def pic2py(picture_names, py_name):"""將圖像文件轉換為py文件:param picture_name::return:"""write_data = []for picture_name in picture_names:filename = picture_name.replace('.', '_')open_pic = open("%s" % picture_name, 'rb')b64str = base64.b64encode(open_pic.read())open_pic.close()# 注意這邊b64str一定要加上.decode()write_data.append('%s = "%s"\n' % (filename, b64str.decode()))f = open('%s.py' % py_name, 'w+')for data in write_data:f.write(data)f.close()if __name__ == '__main__':pics = ["bangpai.jpg", "bangpai_renwu.jpg", "bangpai_renwu2.jpg", "goumai_cw.jpg","goumai_sc.jpg","goumai_yp.jpg","shangjiao_cw.jpg","shangjiao_yp.jpg","shimen.jpg","shimen_songxin.jpg","shiyong.jpg","qiecuo.jpg", "qiecuo_yulin.jpg"]pic2py(pics, 'memory_pic') # 將pics里面的圖片寫到 memory_pic.py 中print("ok")結果是這樣
使用:
在需要使用的地方導入,就可以把bangpai_jpg當作bangpai.jpg來使用。
(不懂的同學可以百度一下或者CSDN一下base64編解碼哈。)
import base64# 從.py文件獲取圖片 def get_pic(pic_code, pic_name):image = open(pic_name, 'wb')image.write(base64.b64decode(pic_code))image.close() # 導入圖片 get_pic(bangpai_jpg, 'bangpai_jpg') get_pic(bangpai_renwu_jpg, 'bangpai_renwu_jpg') get_pic(bangpai_renwu2_jpg, 'bangpai_renwu2_jpg') get_pic(goumai_cw_jpg, 'goumai_cw_jpg') get_pic(goumai_sc_jpg, 'goumai_sc_jpg') get_pic(goumai_yp_jpg, 'goumai_yp_jpg') get_pic(shangjiao_cw_jpg, 'shangjiao_cw_jpg') get_pic(shangjiao_yp_jpg, 'shangjiao_yp_jpg') get_pic(shimen_jpg, 'shimen_jpg') get_pic(shimen_songxin_jpg, 'shimen_songxin_jpg') get_pic(shiyong_jpg, 'shiyong_jpg')結果展示:
打包成功!
運行結果以及日志。
經測試,打寶圖,師門任務,幫派任務全做完了,
在本電腦上測試,效果很好,所有情況都檢測到了,但是換了一臺比較舊的電腦屏幕,偶爾就會有一個按鈕檢測不出來,這時候就需要調整參數或者在本機上抓取圖片來作為基準(因為我們是從屏幕截圖和我們保存的圖的相似度來判斷事件是否發生的,所以屏幕是有關系的比較老的屏幕一般看起來比較暗,所以偶爾檢測不出來也是正常)。
抓鬼的話沒具體做,只是把代碼寫好了沒有填具體參數所以沒有功能。因為覺得組好隊只需要跟著別人就好了。10次才需要點擊一次繼續按鈕。不需要做。當然如果有興趣的同學可以把我的這部分代碼補充完整,然后就可以使用啦。
【注】腳本或程序需要跑在管理員權限運行才行。因為鼠標點擊需要管理員權限。只是截圖不需要。
總結
以上是生活随笔為你收集整理的基于计算机视觉的梦幻西游辅助脚本(只用于开发学习技术)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用PCL库将KITTI数据集可视化
- 下一篇: 市政管理学考试试题及答案