Closed MaxParadiz closed 4 years ago
Unfortunately it's not possible to save anything to another lif file. The format is not an open standard, so it's not realistic to make a program that will write compatible files. If you only need to modify a file or two, I'd recommend using Leica's free LAS X viewer, which will be able to save lif files. Its only available on Windows.
If you are trying to save the output as an RGB TIFF, you can do that with numpy
and PIL
. Here's an example:
from readlif.reader import LifFile
import numpy as np
from PIL import Image
obj = LifFile("./xyzt_test.lif").get_image(0)
obj_c0 = obj.get_frame(z=0, t=0, c=0)
obj_c1 = obj.get_frame(z=0, t=0, c=1)
array_c0 = np.array(obj_c0) # red
array_c1 = np.array(obj_c1) # green
array_c2 = np.zeros(array_c0.shape, dtype=np.uint8) # blue (no data)
# Need to make the grayscale on all RGB channels
array_c3 = np.zeros(array_c0.shape, dtype=np.uint8) + 100 # gray (fake data)
#With equal RGB, this is gray - adjust the 'weight' of each to make different colors
array_gray = np.dstack((array_c3, array_c3, array_c3))
array_rgb = np.dstack((array_c0, array_c1, array_c2)) # make 3D array for RGB
array_rgbc = np.add(array_rgb, array_gray) # add in the grayscale (or other channels)
img = Image.fromarray(array_rgbc)
#img.show()
img.save('output.tiff')
There isn't a good python OME-TIFF writer yet so if you're wanting to import the image into something like ImageJ, it probably would be best to save each channel as an individual image then merge.
If you really want to save something as an OME-TIFF (or a bunch of other things) you can use python-bioformats, but it's a little difficult to work with because it's essentially an interface to the java-based bioformats.
Aha! Well, thanks a lot for your answer and for making this tool available! I've been playing a bit with the PIL format and I might simply be able to analyze the images directly with python rather than saving them for ImageJ :)
I'm glad you find it useful! One of the reasons I chose to read these in as PIL
objects is because they convert really easily to numpy
arrays (see code above) which is handy for machine learning, computer vision, or other kinds of analysis. 😄
I have a large LIF file and I want to extract only a few images. I have managed to select the images that I want, but I have been unable to find a way to save them as individual lif files. It is possible for me to transform them into Pillow objects and then save them, but this leads me to some format issues (I have 4-channel RGB-Gray images). Is there a simple way save a lif image after extracting it from the list?