Ejemplo n.º 1
0
def write_fused(output_path,channel,sizeZ,theC,physX,physY,physZ):

	IJ.log("Writing fused data")

	# number of slices will determine filename format
	digits = "00"
	if sizeZ < 100:
		digits = "0"
	if sizeZ < 10:
		digits = ""

	# get the base metadata from the first fused image
	meta = MetadataTools.createOMEXMLMetadata()
	reader = get_reader(output_path+"img_t1_z%s1_c1"%digits,meta)
	reader.close()
	
	# reset some metadata
	meta.setPixelsPhysicalSizeX(physX,0)
	meta.setPixelsPhysicalSizeY(physY,0)
	meta.setPixelsPhysicalSizeZ(physZ,0)
	meta.setPixelsSizeZ(PositiveInteger(sizeZ),0)
	meta.setChannelID("Channel:0:" + str(0), 0, 0)
	spp = channel['spp']
	meta.setChannelSamplesPerPixel(spp, 0, 0)
	name = channel['name']
	color = channel['color']
	meta.setChannelName(name,0,0)
	meta.setChannelColor(color,0,0)
		
	# determine the number of subsets that need to be written
	slices_per_subset = 200
	num_output_files = divmod(sizeZ,slices_per_subset)
	fpaths = []
	if num_output_files[0] == 0:
		nslices = [sizeZ]
		num_output_files = 1
		fpaths.append("%sfused_C%s.ome.tif"%(output_path,str(theC-1)))
	else:
		nslices = []
		for n in range(num_output_files[0]):
			nslices.append(slices_per_subset)

		if num_output_files[1] > 0:
			nslices.append(num_output_files[1])		
		
		for s in range(len(nslices)):
			fpaths.append("%sfused_C%s_subset%s.ome.tif"%(output_path,str(theC-1),str(s)))

	# setup a writer
	writer = ImageWriter()
	writer.setCompression('LZW')
	writer.setMetadataRetrieve(meta)
	writer.setId(fpaths[0])

	# write the slices, changing the output file when necessary
	theZ = 0
	for f in range(len(fpaths)):
		meta.setImageName(os.path.basename(fpaths[f]),0)
		writer.changeOutputFile(fpaths[f])
		for s in range(nslices[f]):
			fpath = output_path+"img_t1_z%s%s_c1"%(digits,str(theZ+1))
			if (len(digits) == 1) and (theZ+1 > 9):
				fpath = output_path+"img_t1_z%s_c1"%(str(theZ+1))
			if (len(digits) == 2) and (theZ+1 > 9):
				fpath = output_path+"img_t1_z0%s_c1"%(str(theZ+1))
			if (len(digits) == 2) and (theZ+1 > 99):
				fpath = output_path+"img_t1_z%s_c1"%(str(theZ+1))
			IJ.log("writing slice %s"%os.path.basename(fpath))
			m = MetadataTools.createOMEXMLMetadata()
			
			r = get_reader(fpath,m)
			m.setPixelsPhysicalSizeX(physX,0)
			m.setPixelsPhysicalSizeY(physY,0)
			m.setPixelsPhysicalSizeZ(physZ,0)
			m.setChannelID("Channel:0:" + str(0), 0, 0)
			spp = channel['spp']
			m.setChannelSamplesPerPixel(spp, 0, 0)
			name = channel['name']
			color = channel['color']
			m.setChannelName(name,0,0)
			m.setChannelColor(color,0,0)
			writer.saveBytes(theZ,r.openBytes(0))
			r.close()
			theZ += 1
	writer.close()