tiff-label-removal 0.1.0
Remove label pages from TIFF/BigTIFF files at a low level without full file loading.
License: MIT
Requires Python: >=3.7

# TIFF Label Removal Tool

A **Proof of Concept** implementation to remove label pages from TIFF/BigTIFF files  **without loading the entire file into memory** . This repository demonstrates the feasibility of removing specific pages (e.g., label pages) from TIFF files and serves as a foundation for further development.

---

## Features

* Removes **Label** pages from TIFF files.
* Preserves **Macro** and other pages in the file.
* Modifies XML metadata (ImageDescription) to remove label references.
* Operates at a **low-level** file structure, minimizing memory usage.

---

## Installation

1. **Clone** the repository:

   * `git clone https://github.com/<your-username>/tiff-label-removal.git`
   * `cd tiff-label-removal`
2. **Install** dependencies:

   * `python -m pip install -r requirements.txt`

   Ensure you have a compatible Python version (e.g., Python 3.7+).
3. (Optional) **Install** as a package:

   * `pip install .`
     This step allows you to run the tool via a command-line entry point (e.g., `remove-label`) if you have a `setup.py` or `pyproject.toml` configured.

---

## Usage

You can use the script **directly** or via the  **installed CLI** .

### Direct usage

`python remove_label.py <input_tiff_file> <output_tiff_file>`

Example:

`python remove_label.py sample_input.tiff sample_output.tiff`

### CLI usage (if installed)

If you have installed this package locally (via `pip install .`), then run:

`remove-label <input_tiff_file> <output_tiff_file>`

Example:

`remove-label sample_input.tiff sample_output.tiff`

---



## How It Works

1. **Page Verification** : The script reads the TIFF structure via `tifffile`. It identifies pages based on the `ImageDescription` tag, looking for entries with `"Label"` or `"Macro"`.
2. **XML Modification** : For the first page (usually containing metadata), the code extracts the XML, parses it (with `xml.etree.ElementTree`), and removes references to the label information (looking for `PIM_DP_IMAGE_TYPE` = `"LABELIMAGE"`).
3. **Low-Level Copy** : The script copies the file **IFDs (Image File Directories)** and image data segments, but **skips** the label page. It preserves the overall structure (page offsets, IFD chains) so the resulting TIFF remains valid.
4. **Result** : You end up with a TIFF file that excludes the label page but retains other pages and images.

---

## Planned Optimizations

**Performance** :
* Reduce file seeks by collecting and sorting I/O regions.
* Use configurable buffer sizes for copying.
* Merge contiguous byte regions to minimize I/O calls.
  
**IFD Chain Integrity** :
* Perform deeper checks to ensure removing pages doesn’t break references.
* Test against different TIFF variants (e.g., multi-page, BigTIFF).
  
**Comprehensive Testing** :
* Run tests on larger and more diverse TIFF datasets.
* Validate other metadata tags (beyond `ImageDescription`).
  
**Technical Analysis** :
* Further review of TIFF/BigTIFF standards.
* Check how tools like QuPath handle label metadata and page chains.

---

## Contributing

Contributions are welcome!

* **Issues** : If you find a bug or have a question, open a GitHub issue.
* **Pull Requests** : If you’d like to fix something or add a new feature, feel free to create a PR.