-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_zarr_to_png.py
67 lines (50 loc) · 2.11 KB
/
convert_zarr_to_png.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
import zarr
from skimage import exposure
import numpy as np
import cv2
from tqdm import tqdm
from PIL import Image
import os
# Inputs, if saveEvery is 1, all images are saved
acq_cycle = 1
zarr_filename = r'C:\MUSE datasets\REVA\Polymerization Tests\S7\MUSE_stitched_acq_' + str(acq_cycle) + '.zarr'
saveEvery = 25
# Output folder
output_folder = r'C:\MUSE datasets\REVA\Polymerization Tests\S7\PNG images acq ' + str(acq_cycle)
# Gamma contrast adjustments, the structures we are intersted in (fibers) are dark
# Gamma helps increase their brightness
gamma = 0.75
# ---------
# Algorithm
# ---------
# Make the output directory if needed
os.makedirs(output_folder, exist_ok=True)
# Pull array from the zarr dataset
zarr_file = zarr.open(zarr_filename)
muse_data = zarr_file['muse\stitched']
print('Dataset shape:')
print(muse_data.shape)
# Calculate minimum and maximum limits, min and max is 1% percentile
sample_image = np.squeeze(muse_data[0, :, :])
vmin = np.percentile(sample_image, 0.01)
vmax = np.percentile(sample_image, 99.99)
print('Considering ' + str(vmin) + ' ' + str(vmax) + ' as the minimum and maximum intensity limits, the final pngs will be rescaled to 0-255 in this min-max range.')
# Loop through the slices
for k in tqdm(range(0, muse_data.shape[0], saveEvery)):
image = np.squeeze(muse_data[k, :, :])
if np.sum(image) == 0:
print('\n Found empty image, stopping at ' + str(k) + 'images')
break
# Equivalent to setting the min max values on the histogram
image[image > vmax] = vmax
image[image < vmin] = vmin
image = cv2.normalize(image, None, alpha = 0, beta = 255, norm_type = cv2.NORM_MINMAX, dtype = cv2.CV_32F)
image[image > 255] = 255
image[image < 0] = 0
# Setting the gamma
image = exposure.adjust_gamma(image, gamma = gamma)
# Normalizing this back to 0-255
image = np.floor(cv2.normalize(image, None, alpha = 0, beta = 255, norm_type = cv2.NORM_MINMAX, dtype = cv2.CV_32F)).astype('uint8')
# Save the image as png
pillow_image = Image.fromarray(image)
pillow_image.save(output_folder + r'\\Image_' + str(k).zfill(5) + '.png')