Interactive Python script for measuring tissue area from TIFF images with a fixed microscope scale bar.
The workflow is designed for semi-automatic measurement:
- Load one TIFF image or a folder of TIFF images.
- Use a fixed scale bar length in pixels or calibrate once from the first image.
- Click one point inside the target tissue for each image.
- Refine the segmentation with an OpenCV brush editor.
- Export per-image mask and overlay previews, plus a single summary CSV.
- Supports
.tifand.tiffinput - Supports single-image and batch folder processing
- Fixed scale bar pixel mode to skip manual scale calibration
- Measures full outer contour area, including internal holes
- OpenCV brush editing for manual erase/add correction
- Writes one summary CSV for all processed images
- Python 3.9+
numpyopencv-pythonmatplotlibscikit-imagetifffile
Install dependencies:
python -m pip install -r requirements.txtProcess a single image:
python tissue_area_interactive.py path/to/image.tif --scale-um 100 --fixed-scale-pixels 194Process a folder of TIFF images:
python tissue_area_interactive.py path/to/folder --scale-um 100 --fixed-scale-pixels 194If --fixed-scale-pixels is omitted, the script asks you to click the two ends of the scale bar on the first image.
After automatic segmentation, an OpenCV editor window opens:
- Left mouse drag: erase mask area
- Right mouse drag: add mask area
+/-: change brush sizer: reset to the automatic segmentationEnter: confirm the current mask and continueEsc: close the editor and continue with the current mask
The brush starts at 80 px by default.
The script writes results to outputs/ by default:
*_mask.png: binary mask*_overlay.png: overlay with contour*_preview.png: three-panel previewsummary_measurements.csv: combined results for all processed images
The summary CSV contains:
imagepixel_areaum_per_pixelarea_um2area_mm2
Some TIFF files contain multiple pages:
- page 0: the full-resolution image
- page 1: a low-resolution thumbnail used by Preview
To batch-export only the first page into single-page TIFF files:
python extract_first_tiff_page.py path/to/folder --output-dir extracted_tiffsIf the TIFFs are inside nested subfolders and you want to keep the same folder structure in the output directory:
python extract_first_tiff_page.py path/to/folder --output-dir extracted_tiffs --recursiveTo process a single file:
python extract_first_tiff_page.py path/to/image.tif --output-dir extracted_tiffsIf you want to overwrite existing outputs:
python extract_first_tiff_page.py path/to/folder --output-dir extracted_tiffs --overwrite- Area is measured using the full outer contour of the tissue.
- Internal dark holes are filled and counted as part of the tissue area.
- The fixed scale bar mode is useful when every image uses the same scale bar size.