-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZY_CreatTemp_V2.py
87 lines (72 loc) · 2.04 KB
/
ZY_CreatTemp_V2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#Author: ZY Ye
#Date: 09/22/2016
#Creat Temp for time series file alignment
#1. choose file
#2. select slice for temp
#3. dupplicate the target slice
#4. duplicate target slice to nslice stack: copy then add/paste root
#5. save as temp.tif in temp folder
import os
from ij import IJ, ImagePlus
from ij.io import FileSaver
import sys
import re
def run():
printLog("=====ZY_CreatTemp_V2====",0)
#Prompt user to open a image
od = OpenDialog("Choose image file", None)
if od is None:
msgStr = "User Cancled"
printLog(msgStr,1)
else:
sourceFolder = od.getDirectory()
fileName = od.getFileName()
imp = IJ.openImage(sourceFolder+fileName)
imp.show()
n = imp.getNSlices()
printLog("Processing source file: " + fileName,1)
imp2= pickSlice(imp)
if imp2:
destFolder = os.path.join(sourceFolder, 'Temps')
#outName = os.path.join(destFolder,fileName[:-4]+'_temp.tif') #remove the .tif in filename
outName = os.path.join(destFolder,'temp.tif')
# check or make the folder
if not os.path.isdir(destFolder):
os.makedirs(destFolder)
#make temp
dupNSlice(imp2,n-1)
printLog("Saving to: " + outName,1)
fs = FileSaver(imp2)
fs.saveAsTiffStack(outName)
imp2.close()
imp.close()
msgStr = "ZY_CraetTemp_V2.py is Done."
printLog(msgStr,0)
def pickSlice(imp):
gd = NonBlockingGenericDialog("pick a slice") #non-modal dialog will not block selecting others
#gd.centerDialog(0) #this could enable the setLocation
#gd.setLocation(380,0)
gd.addNumericField('Target slice', 1, 0)
gd.showDialog()
if gd.wasCanceled():
printLog("Mission Cancelled by user",0)
return
else:
pickN = int(gd.getNextNumber())
imp.setSlice(pickN) ##
IJ.run(imp,"Duplicate...", "title=Temp")
imp2=IJ.getImage()
return imp2
def dupNSlice(imp,n):
IJ.run(imp,"Copy","")
for i in range(n):
IJ.run(imp,"Add Slice","")
IJ.run(imp,"Paste","")
def printLog(text, indent):
msgStr = ''
for i in (range(indent)):
msgStr += ' '
print msgStr,text #to command line
IJ.log(msgStr + text)
if __name__ == '__main__':
run()