-
Notifications
You must be signed in to change notification settings - Fork 2
/
vsi2tif.py
65 lines (44 loc) · 1.83 KB
/
vsi2tif.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
import numpy as np
import subprocess as sp
from tqdm import tqdm
import os
import matplotlib.pyplot as plt
from pytictoc import TicToc
import javabridge as jb
import bioformats as bf
# initialize timer
t = TicToc()
t.tic()
# choose image to convert
loc = "/mnt/EncryptedData2/pathology/images/126.vsi"
# output path
out_path = "/mnt/EncryptedPathology/pathology/sandbox/convert_test/"
# path to bfconvert command line tool
bfconvert_path = "/home/andrep/DP/python/bftools/bfconvert"
# set memory limit # <- DIDN'T WORK BETWEEN SUBPROCESSES(!)
#process = sp.Popen("export BF_MAX_MEM=0.2g".split(), stdout=sp.PIPE, shell=True)
#output, error = process.communicate()
# current image
image = loc.split("/")[-1].split(".")[0]
# params
plane = 0 # to convert all (cut-off, all images planes larger and equal to this value are considered from the image)
tz = 1024 # tile size
# compression methods
comp1 = "LZW" # "JPEG-2000" #JPEG #"LZW"
comp2 = "jpeg" # "jpeg" #"lzw", "jpeg", "deflate" (zip), "none" # <----- Best to use zip-conversion here(?)
Q2 = 85 # default: 75 (quality of compression)
# output paths (mid-step and final converted and compressed output)
out1 = out_path + image + "_plane_" + str(plane) + "_cm_" + comp1 + ".btf"
out2 = out_path + image + "_test" + "_plane_" + str(plane) + "_cm_" + comp1 +'_' + comp2 + '_Q_' + str(Q2) + ".tif"
# vsi -> btf
sp.check_call(["sh", bfconvert_path, "-tilex", str(tz), "-tiley", str(tz), "-nogroup", "-no-upgrade",
"-overwrite", "-bigtiff", "-series", str(plane), loc, out1])
# -compression, comp1 <- choose no compression instead in this mid-step
# btf -> tif
sp.check_call(["vips", "tiffsave", out1, out2, "--bigtiff", "--tile", "--pyramid",\
"--compression=" + comp2, "--Q=" + str(Q2)])
# delete btf file
os.remove(out1)
# kill virtual machine
jb.kill_vm()
t.toc()