-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index2MaxProjPNG.py
42 lines (37 loc) · 1.48 KB
/
Index2MaxProjPNG.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
import numpy as np
import os
import sys
import nrrd, png
from PIL import Image
def thumbnail(file, size=(25,25)):
im = Image.open(file)
im = im.resize(size, Image.ANTIALIAS)
tnFile = file.replace('.png','_tn.png')
im.save(tnFile,"PNG")
return True
if (len(sys.argv) < 4):
print 'Error: missing arguments!'
print 'e.g. python Index2MaxProjPNG.py template DomainPrefix indexfile1.nrrd indexfileN.nrrd ...'
else:
print 'Loading template...'
data1, header1 = nrrd.read(str(sys.argv[1]))
template=np.array((np.max(data1,axis=2)*0.5),dtype=np.uint8)
del data1, header1
print 'Adding to domains', str(sys.argv[2]), '....'
for x in range(3,(len(sys.argv))):
print 'adding data from file', sys.argv[x]
readdata, options = nrrd.read(str(sys.argv[x]))
for i in np.unique(readdata[readdata>0]):
if np.uint8(i) in readdata:
print 'appending index', str(i)
domfile = str(sys.argv[2]) + str(i).zfill(4) + '.png'
domain = np.zeros(readdata.shape,dtype=np.uint8)
domain[readdata==i]=np.uint8(255)
png1=np.max((np.transpose((np.max(domain,axis=2))), template.T),axis=0)
png.from_array(png1,'L').save(domfile)
if np.shape(png1)[0] < np.shape(png1)[1]:
thumbnail(domfile, size=(120,60))
else:
thumbnail(domfile, size=(60,120))
del domain, png1
print 'Done.'