-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpy2dzi.py
90 lines (77 loc) · 2.75 KB
/
npy2dzi.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
import fire
import numpy as np
import PIL
from PIL import Image
import os
import sys
import deepzoom
import time
import shutil
def main(wsi_name, web_dir, shrink_factor=1):
# disable safety checks for large images
PIL.Image.MAX_IMAGE_PIXELS = None
assert(wsi_name[-4:] == ".npy")
wsi_prefix = wsi_name[:-4]
prefix_path = os.path.join(web_dir, "images", wsi_prefix)
npy_path = prefix_path + ".npy"
png_path = prefix_path + ".png"
dzi_path = prefix_path + ".dzi"
base_html_path = "npy2dzi.html"
new_html_path = os.path.join(web_dir, "index.html")
openseadragon_src = "openseadragon/"
openseadragon_dst = os.path.join(web_dir, "openseadragon/")
iter_name = web_dir[web_dir.rindex("/") + 1:]
title = iter_name + " " + wsi_name
START_TIME = time.time()
print("Loading .npy file")
img = np.load(npy_path)
print("Execution time (s):", time.time() - START_TIME)
print("Done.\n")
START_TIME = time.time()
print("Reducing .npy file")
if shrink_factor == 1:
comp_img = img
else:
comp_img = np.zeros((img.shape[0] // shrink_factor, img.shape[1] // shrink_factor, img.shape[2]), dtype=np.uint32)
print(comp_img.shape)
for i in range(shrink_factor):
j1 = img.shape[0] - img.shape[0] % shrink_factor
j2 = img.shape[1] - img.shape[1] % shrink_factor
comp_img += img[i:j1:shrink_factor, i:j2:shrink_factor]
comp_img //= shrink_factor
print("Execution time (s):", time.time() - START_TIME)
print("Done.\n")
# create png files
START_TIME = time.time()
print("Creating .png file")
Image.fromarray(comp_img.astype(np.uint8)).save(png_path, compress_level=1)
print("Execution time (s):", time.time() - START_TIME)
print("Done.\n")
# create dzi files
START_TIME = time.time()
print("Creating .dzi file")
creator = deepzoom.ImageCreator(
tile_size=256,
tile_overlap=0,
tile_format="png",
image_quality=1.0,
)
creator.create(png_path, dzi_path)
print("Execution time (s):", time.time() - START_TIME)
print("Done.\n")
START_TIME = time.time()
print("Creating HTML files")
# create html files
with open(base_html_path, "r") as f:
HTML_STR = "".join(f.readlines())
HTML_STR = HTML_STR.replace("{REPLACE_wsi_prefix}", os.path.join("images", wsi_prefix))
HTML_STR = HTML_STR.replace("{REPLACE_title}", title)
with open(new_html_path, "w") as f:
f.write(HTML_STR)
# copy openseadragon
if not os.path.isdir(openseadragon_dst):
shutil.copytree(openseadragon_src, openseadragon_dst)
print("Execution time (s):", time.time() - START_TIME)
print("Done.\n")
if __name__=="__main__":
fire.Fire(main)