Python + imageJ 解决方案,采坑记录
方案一:ImageJ的Python脚本编程
方案二:jython
建议放弃,因为jython很多包都pip install 不了
方案三:pyimagej(注意不是imagepy,imagepy的作者是闫大),
方案四:Python程序,cmd调用imageJ,解析imageJ的宏脚本,是可以做到的
---
code如下
def WriteImageJMacroFile(fileRootPath, fileName, ROI): fileRootPath = fileRootPath.replace('\\', '/') ijmPath = fileRootPath + "/" + fileName[0:-4] + '.ijm' p1 = "fileRootPath=\"" + fileRootPath + "\";\n" p2 = "fileName=\"" + fileName + "\";\n" p3 = "ROI=newArray(" + str(ROI.iloc[0, 0]) + "," + str(ROI.iloc[1, 0]) + "," + str(ROI.iloc[2, 0]) + "," + str( ROI.iloc[3, 0]) + ");\n" body = """function processFile(fileRootPath, fileName) { setBatchMode(true); open(fileRootPath + "/" + fileName); id = getImageID(); selectImage(id); makeRectangle(ROI[0], ROI[1], ROI[2], ROI[3]); run("Crop"); run("8-bit"); run("Size...", "width=100 height=80 average interpolation=Bilinear"); outputFileName2 = "Pupil_Crop_"+fileName.substring(0,lengthOf(fileName)-4)+".avi"; saveAs("avi", fileRootPath+"/"+outputFileName2); run("Close"); } processFile(fileRootPath,fileName);""" macro2 = p1 + p2 + p3 + body with open(ijmPath, "w") as f: f.write(macro2) return ijmPath