本文来自我的个人博客 https://www.zhangshenghai.com/posts/42676/
昨天在帮师兄做脑核磁共振图像的颅骨去除,使用的是一个桌面软件,需要输入所需要的文件,然后软件自动进行分割,手动做的话每例大概需要三四分钟,共两百多例,这样效率太低了,且实在是很不优雅。于是决定用python写一个脚本来自动模拟鼠标操作,使用的是pyautogui库,特此记录以便参考。
首先进入base环境安装pyautogui库:
activate base
conda install -c jim-hart pyautogui
因为需要输入的是4个nii文件,所以首先删除不需要的文件,保证文件夹内的文件数量:
src_dir = r"F:\0"
need_file = ['t1c.nii', 't2.nii', 't1.nii', 'flair.nii']
for dir in os.listdir(src_dir):
dst_dir = os.path.join(src_dir, dir)
for file in os.listdir(dst_dir):
if (file not in need_file):
os.remove(os.path.join(dst_dir, file))
然后判断需要的4个文件是否都存在,去除不满足条件的病例。
src_dir = r"F:\0"
need_file = ['t1c.nii', 't2.nii', 't1.nii', 'flair.nii']
for dir in os.listdir(src_dir):
dst_dir = os.path.join(src_dir, dir)
filenum = len([lists for lists in os.listdir(dst_dir)
if os.path.isfile(os.path.join(dst_dir, lists))])
if (filenum != 4):
print(dst_dir)
使用脚本控制鼠标太快了,可能会导致应用出问题,为了减缓鼠标点击速度,pyautogui提供了中断措施,为所有的pyatutogui函数增加延迟,默认的延迟是0.1s,这里做如下调整:
pyautogui.PAUSE = 0.6
接着确定各个按钮的坐标,坐标的位置可以用以下代码确定:
x, y = pyautogui.position()
print(x, y)
这里用到的鼠标操作主要是移动、单击、双击和多次点击:
pag.moveTo(x,y)
pyautogui.click()
pyautogui.doubleClick()
pyautogui.click(clicks=10, interval=0.25)
本来是有用到滚轮操作的,但是不知道什么原因,在代码中失效,也就放弃了这个操作,用多次点击代替。pyautogui这个库还是很强大的,也支持键盘操作,只不过这里不需要键盘操作也就没有用到。
比较简单地介绍了使用python模拟鼠标(键盘)操作,以下是具体的源码:
import pyautogui as pag
import os
import time
screenWidth, screenHeight = pag.size()
assert screenWidth == 1920
assert screenHeight == 1080
x, y = pag.position()
print(x, y)
pag.moveTo(700, 700)
pag.PAUSE = 0.6
pag.moveTo(folder_rightx, folder_righty)
# pag.click(clicks=1, interval=0.25)
loadx, loady = 18, 32
patientx, patienty = 71, 56
outputx, outputy = 1247, 666
Fdiskx, Fdisky = 724, 425
folderx, foldery = 695, 443
folder_rightx, folder_righty = 981, 359
t1_extrax, t1_extray = 712, 441
t1x, t1y = 956, 380
t1cx, t1cy = 956, 380+17
t2x, t2y = 956, 380+35
flairx, flariy = 956, 380-17
load2x, load2y = 700, 700
performx, performy = 121, 404
next1x, next1y = 900, 724
next2x, next2y = 1207, 742
# set T1 as baseline
blx, bly = 1230, 444
for i in range(0, 1):
print("current No: %d" % i)
try:
# open add patient
pag.moveTo(loadx, loady)
pag.click()
pag.moveTo(patientx, patienty)
pag.click()
# open output folder
pag.moveTo(outputx, outputy)
pag.click()
pag.moveTo(Fdiskx, Fdisky)
pag.doubleClick()
pag.moveTo(folderx, foldery)
pag.doubleClick()
if i == 0:
pag.moveTo(folder_rightx, folder_righty)
else:
pag.moveTo(next2x, next2y)
pag.click(clicks=i, interval=0.25)
pag.moveTo(folder_rightx, folder_righty)
pag.doubleClick()
# set T1 path
pag.moveTo(blx, bly)
pag.click()
if i == 0:
pag.moveTo(t1_extrax, t1_extray)
else:
pag.moveTo(next1x, next1y)
pag.click(clicks=i, interval=0.25)
pag.moveTo(t1_extrax, t1_extray)
pag.doubleClick()
pag.moveTo(t1x, t1y)
pag.doubleClick()
# set T1c path
pag.moveTo(blx, bly+38*1)
pag.click()
pag.moveTo(t1cx, t1cy)
pag.doubleClick()
# set t2 path
pag.moveTo(blx, bly+38*2)
pag.click()
pag.moveTo(t2x, t2y)
pag.doubleClick()
# set Flair path
pag.moveTo(blx, bly+38*3)
pag.click()
pag.moveTo(flairx, flariy)
pag.doubleClick()
# set Template (same as T1c)
pag.moveTo(blx, bly+38*4)
pag.click()
pag.moveTo(t1cx, t1cy)
pag.doubleClick()
# load
pag.moveTo(load2x, load2y)
pag.click()
time.sleep(3)
# perform
pag.moveTo(performx, performy)
pag.click()
# performing
#time.sleep(60)
except:
print("No.%d is Error" % i)
# 删除不需要的文件,保证文件夹内文件数量小于等于4
src_dir = r"F:\0"
need_file = ['t1c.nii', 't2.nii', 't1.nii', 'flair.nii']
for dir in os.listdir(src_dir):
dst_dir = os.path.join(src_dir, dir)
for file in os.listdir(dst_dir):
if (file not in need_file):
os.remove(os.path.join(dst_dir, file))
# 判断需要的四个文件是否都存在
src_dir = r"F:\0"
need_file = ['t1c.nii', 't2.nii', 't1.nii', 'flair.nii']
for dir in os.listdir(src_dir):
dst_dir = os.path.join(src_dir, dir)
filenum = len([lists for lists in os.listdir(dst_dir)
if os.path.isfile(os.path.join(dst_dir, lists))])
if (filenum != 4):
print(dst_dir)
网友评论