import czifile as cz # You will first have to install this library by typing “pip install czifile”
import xml.etree.ElementTree as ET
import tifffile as tf
import tkinter as tk
from tkinter import filedialog
from tkinter import simpledialog
import os
#Gets the path to the image.
root = tk.Tk()
root.withdraw()
filepath_sample = filedialog.askopenfilename(title = “Select the sample image”) # This line creates a dialog window that allows the user to select the image.
#This manipulates the path to the image.
string_sample = os.path.splitext(filepath_sample)[0]
filename_sample = os.path.basename(string_sample) # This gets the actual filename without the .czi extension.
#Gets the metadata.
with cz.CziFile(filepath_sample) as czi:
xml_metadata = czi.metadata()
#find timestamps (this is relevant only if you have a time series, but you don’t have one because the fourth axis in your .czi file is empty)
#for attachment in czi.attachments():
#if attachment.attachment_entry.name == ‘TimeStamps’:
#timestamps = attachment.data()
#break
#This gets the resolution of the image in microns.
root = ET.fromstring(xml_metadata)
for neighbor in root.iter(‘ScalingX’):
pixel_size_in_meters = neighbor.text
pixel_size_in_microns = float(pixel_size_in_meters)*1000000
resolution_x = 1/pixel_size_in_microns
#This gets the time between images in seconds. Not relevant in your case because you don’t have a time series, as mentioned above.
#time_between = int(round((timestamps[1]-timestamps[0])))
#This imports the image.
img_sample = cz.imread(filepath_sample)
#Gets rid of unnecessary info (in your case the third axis is the number of channels, the fifth is the Z plane (from a Z-stack), the sixth is the X pixels, the seventh is the Y pixels). All the rest is rubbish.
img_sample = img_sample[0,0,:,0,:,:,:,0] # : means import all values. 0 means discard.
#Saves the image as a tiff for ImageJ.
tf.imwrite(filename_sample+’_for_imageJ.tiff’, img_sample, imagej=True, resolution=(resolution_x, resolution_x), metadata={‘spacing’: 1.0, ‘unit’: ‘micron’})
#This website messes with the " and the ’ and with the indentations. You’ll have to correct that to use this script in Python.