ImageJ:如何让宏设置的比例尺参数在Scale Bar菜单中保留?
解决ImageJ Scale Bar对话框重置参数的问题
问题描述
我有一套用于选择显微镜拍摄图像的放大倍率、设置比例尺并添加预设外观比例尺条的宏,存储在ImageJ宏目录下的StartupMacros.txt文件中,代码如下:
macro "Scale_Leica_010x_whitebg [1]" { run("Set Scale...", "distance=3021 known=1000 unit=µm global"); run("Scale Bar...", "width=500 height=39 thickness=10 font=70 color=Gray background=None location=[Lower Left] horizontal bold overlay"); } macro "Scale_Leica_020x_whitebg [2]" { run("Set Scale...", "distance=4260 known=700 unit=µm global"); run("Scale Bar...", "width=200 height=39 thickness=10 font=70 color=Gray background=None location=[Lower Left] horizontal bold overlay"); }
执行宏后,若需要修改比例尺条的位置等参数,打开Analyze>Tools>Scale Bar菜单时,所有设置会重置为默认值,需重新调整颜色、字号等参数,十分繁琐。希望打开该菜单时,宏预设的参数已填充到对应字段,仅修改需要调整的项。
解决方法
ImageJ的Scale Bar对话框参数读取自软件偏好设置,只需在宏中添加代码将预设参数写入偏好,即可让对话框打开时自动加载这些参数。
方法1:修改现有宏,执行时同步保存偏好
修改你的StartupMacros.txt中的宏,添加偏好设置代码,执行宏时不仅添加比例尺条,还会将参数保存到ImageJ偏好:
macro "Scale_Leica_010x_whitebg [1]" { // 设置比例尺 run("Set Scale...", "distance=3021 known=1000 unit=µm global"); // 添加比例尺条 run("Scale Bar...", "width=500 height=39 thickness=10 font=70 color=Gray background=None location=[Lower Left] horizontal bold overlay"); // 将预设参数写入偏好 Prefs.set("scalebar.width", 500); Prefs.set("scalebar.height", 39); Prefs.set("scalebar.thickness", 10); Prefs.set("scalebar.fontSize", 70); Prefs.set("scalebar.color", "Gray"); Prefs.set("scalebar.background", "None"); Prefs.set("scalebar.location", "Lower Left"); Prefs.set("scalebar.horizontal", true); Prefs.set("scalebar.bold", true); Prefs.set("scalebar.overlay", true); Prefs.savePreferences(); // 保存偏好到本地文件 } macro "Scale_Leica_020x_whitebg [2]" { // 设置比例尺 run("Set Scale...", "distance=4260 known=700 unit=µm global"); // 添加比例尺条 run("Scale Bar...", "width=200 height=39 thickness=10 font=70 color=Gray background=None location=[Lower Left] horizontal bold overlay"); // 将预设参数写入偏好 Prefs.set("scalebar.width", 200); Prefs.set("scalebar.height", 39); Prefs.set("scalebar.thickness", 10); Prefs.set("scalebar.fontSize", 70); Prefs.set("scalebar.color", "Gray"); Prefs.set("scalebar.background", "None"); Prefs.set("scalebar.location", "Lower Left"); Prefs.set("scalebar.horizontal", true); Prefs.set("scalebar.bold", true); Prefs.set("scalebar.overlay", true); Prefs.savePreferences(); // 保存偏好到本地文件 }
修改后,每次执行宏时,对应的比例尺条参数会被写入偏好。之后打开Scale Bar对话框,所有预设参数会自动填充,只需修改需要调整的项即可。
方法2:创建单独宏,直接加载预设并打开对话框
如果不需要每次添加比例尺条都同步偏好,可创建单独宏,用于快速加载对应预设并打开Scale Bar对话框:
macro "Open_10x_Scalebar_Dialog" { // 加载10x比例尺条预设到偏好 Prefs.set("scalebar.width", 500); Prefs.set("scalebar.height", 39); Prefs.set("scalebar.thickness", 10); Prefs.set("scalebar.fontSize", 70); Prefs.set("scalebar.color", "Gray"); Prefs.set("scalebar.background", "None"); Prefs.set("scalebar.location", "Lower Left"); Prefs.set("scalebar.horizontal", true); Prefs.set("scalebar.bold", true); Prefs.set("scalebar.overlay", true); Prefs.savePreferences(); // 打开Scale Bar对话框,参数已填充 run("Scale Bar..."); } macro "Open_20x_Scalebar_Dialog" { // 加载20x比例尺条预设到偏好 Prefs.set("scalebar.width", 200); Prefs.set("scalebar.height", 39); Prefs.set("scalebar.thickness", 10); Prefs.set("scalebar.fontSize", 70); Prefs.set("scalebar.color", "Gray"); Prefs.set("scalebar.background", "None"); Prefs.set("scalebar.location", "Lower Left"); Prefs.set("scalebar.horizontal", true); Prefs.set("scalebar.bold", true); Prefs.set("scalebar.overlay", true); Prefs.savePreferences(); // 打开Scale Bar对话框,参数已填充 run("Scale Bar..."); }
将这些宏添加到StartupMacros.txt中,需要调整比例尺条时,直接运行对应的宏即可打开带有预设参数的对话框。
内容的提问来源于stack exchange,提问作者Gilles San Martin
相关产品推荐
相关产品推荐

