/
czi_to_tif.py
144 lines (130 loc) · 4.74 KB
/
czi_to_tif.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#@UIService uiService
#@LogService log
#-----------------------DESCRIPTION--------------------------------------------------------------------
#adapted from sbesson czi.py script (https://gist.github.com/sbesson/42bcb21dc30f3644c17aabb6f5a1d917)
#run in FIJI
#requires empty output folder
#requires imput folder with all the czi files that you want to convert
#Author: Brigida Rusconi
#Date: 05/09/2018
#Version: 0.1
#-----------------------DESCRIPTION--------------------------------------------------------------------
# read in and display ImagePlus object(s)
from loci.plugins import BF
from loci.common import Region
from loci.plugins.in import ImporterOptions
from loci.plugins.util import LociPrefs
from ij import Prefs
from loci.formats import ImageReader
from loci.formats import MetadataTools
from ij import IJ
from ij import ImagePlus
from ome.units import UNITS
import os, sys
# ZEISS STUFF
from loci.formats.in import ZeissCZIReader
from loci.formats.in import DynamicMetadataOptions
def getImageSeries(imps, series):
imp = imps[series]
# get the stack
imgstack = imp.getImageStack()
slices = imgstack.getSize()
log.info("Show Series # : " + str(series))
log.info("ImgStack.getSize() : " + str(slices))
return imp
#get total amount of scenes
def getTotal(imagefile,setflat=True):
options = DynamicMetadataOptions()
options.setBoolean("zeissczi.attachments", False)
czireader = ZeissCZIReader()
czireader.setFlattenedResolutions(setflat)
czireader.setId(imagefile)
seriesCount = czireader.getSeriesCount()
czireader.close()
return seriesCount
#save to tiff
def getCZIinfo(imagefile, showimage=False, setreslevel=0, setflat2=False, openallseries=True, showomexml=False,setconcat=False,filepath1="./"):
options = DynamicMetadataOptions()
options.setBoolean("zeissczi.attachments", False)
czireader = ZeissCZIReader()
czireader.setFlattenedResolutions(setflat2)
czireader.setMetadataOptions(options)
czireader.setId(imagefile)
lc = czireader.getSeriesCount()
#get the first occurence of each pyramid stack
location=list()
for i in range(0, int(seriesCount)-2):
location.append(czireader.coreIndexToSeries(i))
c=0
#log.info(location)
loc2=list()
for i,v in enumerate(location):
if i==0:
loc2.append(i)
elif i>0 and v!=c:
loc2.append(i)
c=v
log.info(str(loc2))
# get OME data
omeMeta = MetadataTools.createOMEXMLMetadata()
# Set the preferences in the ImageJ plugin
Prefs.set("bioformats.zeissczi.include.attachments", str(True).lower())
if showimage:
# read in and display ImagePlus(es) with arguments
options = ImporterOptions()
options.setOpenAllSeries(openallseries)
options.setShowOMEXML(showomexml)
options.setConcatenate(setconcat)
options.setId(imagefile)
# open the ImgPlus
imps = BF.openImagePlus(options)
name_list=imagefile.split('/')
name=name_list[len(name_list)-1]
out_path=filepath1 + "/"+name+"_Preview.tif"
log.info(name)
imp=getImageSeries(imps, seriesCount-1)
imp.show()
IJ.run("RGB Color")
imp.close()
IJ.saveAs("tiff", out_path)
IJ.run("Close")
out_path=filepath1 + "/"+name+"_Label.tif"
imp=getImageSeries(imps, (seriesCount-2))
imp.show()
IJ.run("RGB Color")
imp.close()
IJ.saveAs("tiff", out_path)
IJ.run("Close")
c=1
for series in loc2:
out_path=filepath1 + "/"+name+"Scene_" + str(c) + ".tif"
imp=getImageSeries(imps, series)
imp.show()
IJ.run("RGB Color")
imp.close()
IJ.saveAs("tiff", out_path)
IJ.run("Close")
c+=1
czireader.close()
# clear the console automatically when not in headless mode
uiService.getDefaultUI().getConsolePane().clear()
inputDir = IJ.getDir("Choose input Directory")
#only grabs files in case there are any folders in the directory
#https://stackoverflow.com/questions/11968976/list-files-in-only-the-current-directory
dir_array1 = [f for f in os.listdir(inputDir) if os.path.isfile(os.path.join(inputDir,f))]
#does not generate a tiff file or folder for the thumbnail pictures of the preview and label
dir_array=[f for f in dir_array1 if "pt" not in f]
log.info(dir_array1)
log.info(dir_array)
dir_length = len(dir_array)
#create an output directory inside the folder with all the other ones
for i,image in enumerate(dir_array):
fp=inputDir + "output"
if not os.path.exists(fp):
os.mkdir(fp)
filepath=fp+"/" + dir_array[i]
if not os.path.exists(filepath):
os.mkdir(filepath)
imag= inputDir + image
seriesCount=getTotal(imag)
getCZIinfo(imag,showimage=True,filepath1=filepath)