I am trying to save an altered z-stack .tif file in Python3. Here's my code where I checked that the the functions worked as intended.
#libraries
import imageio as ii
#import initial image
fname='101_nuc1syg1.tif'
adata = ii.volread(fname)
#check to make sure volread works
ii.volsave('temp.tif', adata)
Which results in this:
And now when I try to do a simple threshold, using the following code:
#now doing very simple thresholding
bdata = adata < adata[0].mean()
bdata = bdata +0
ii.volsave('temp.tif', bdata)
I get this:
Any idea how to save a tif file properly after performing image operators on it?
EDIT: Note that I am able to extract each stack and save them as separate .png files, but I would prefer to have them as a single .tif file.
Data from: https://www.nature.com/articles/s41467-020-15987-2
It looks like you need to convert bdata
type to np.uint8
and multiply the result by 255.
The type of the expression (adata < adata[0].mean()) is np.bool
.
The common image type is np.uint8
.
Convert (adata < adata[0].mean())
to type np.uint8
:
bdata = (adata < adata[0].mean()).astype(np.uint8)
When converting the result to np.uint8
, all True
elements are converted to 1
and False elements are converted to 0
.
Multiply by 255
for converting the True
elements to 255
(white color):
bdata = bdata * 255
Complete code:
import imageio as ii
import numpy as np
#import initial image
fname='101_nuc1syg1.tif'
adata = ii.volread(fname)
#now doing very simple thresholding
bdata = (adata < adata[0].mean()).astype(np.uint8)
bdata = bdata * 255 # Convert ones to 255 (255 is white color)
ii.volsave('temp.tif', bdata)
Note:
I could not test my answer - the link you have posted doesn't contain a link to an image.