-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadTiff.py
105 lines (91 loc) · 3.47 KB
/
readTiff.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
from PIL import Image
import os,glob
import numpy as np
import shutil
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import misc, ndimage, io
import ntpath
ntpath.basename("a/b/c")
import json
def readTiff(filepath, onlyOneFile=True, axis=0,updater=None, colormap='Greys',invert=False, folder=False):
try:
shutil.rmtree("static/data/test")
except:
pass
filename = "test"
try:
os.mkdir("static/data")
except:
pass
try:
os.mkdir("static/data/"+filename)
except:
pass
if folder:
try:
list_files = []
for file in os.listdir(filepath):
if file.endswith(".tiff") or file.endswith(".tif"):
list_files.append(os.path.join(filepath, file))
im = Image.open(list_files[0])
h, w = im.width, im.height
tiffarray = np.zeros((w, h, len(list_files), 4))
cm_hot = mpl.cm.get_cmap(colormap + ("_r" if invert else ""))
os.mkdir("static/data/" + filename + "/" + str(0))
for n,i in enumerate(list_files):
im = Image.open(i)
image1 = np.array(im)
image1 = cm_hot(image1)
image1 = np.uint8(image1 * 255)
# image1 = misc.fromimage(im, flatten=1)
tiffarray[:, :, n] = np.array(image1)
# misc.imsave("static/data/test/0/"+str(im.tell())+".png", image1)
except EOFError:
pass # end of sequence
else:
try:
im = Image.open(filepath)
h, w = im.width, im.height
tiffarray = np.zeros((w, h, im.n_frames, 4))
cm_hot = mpl.cm.get_cmap(colormap + ("_r" if invert else ""))
os.mkdir("static/data/" + filename + "/" + str(0))
for i in range(im.n_frames):
im.seek(im.tell() + 1)
image1 = np.array(im)
image1 = cm_hot(image1)
image1 = np.uint8(image1 * 255)
# image1 = misc.fromimage(im, flatten=1)
tiffarray[:, :, i] = np.array(image1)
# misc.imsave("static/data/test/0/"+str(im.tell())+".png", image1)
except EOFError:
pass # end of sequence
dim = tiffarray.shape
if onlyOneFile:
slice = simple_slice(tiffarray, math.floor(dim[axis] / 2), axis)
misc.imsave("static/data/"+filename+"/"+str(1)+".png", slice)
else:
for i in range(20, dim[axis]+1):
updater.setValue((i/(dim[axis]+1))*100)
misc.imsave("static/data/"+filename+"/"+str(i-20)+".png", simple_slice(tiffarray, i-1, axis))
infoFile = {"numImages":dim[axis]+1-20, "height":simple_slice(tiffarray, 0, axis).shape[0], "width":simple_slice(tiffarray, 0, axis).shape[1]}
with open("static/test.js", 'w') as outfile:
outfile.write("var configuration ="+str(json.dumps(infoFile)))
# print(tiffarray)
return filename
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
def simple_slice(arr, inds, axis):
# this does the same as np.take() except only supports simple slicing, not
# advanced indexing, and thus is much faster
sl = [slice(None)] * arr.ndim
sl[axis] = inds
return arr[sl]
# readTiff("butterfly_wing_small.tif")
filepath = "C:\\Users\\gadge\\Downloads\\als\\New Folder With Items"
colormap="Greys"
filename ="test"
invert =True