Skip to content

Instantly share code, notes, and snippets.

@ksugar
Last active April 3, 2021 15:50
Show Gist options
  • Save ksugar/db5eb3a97397092749db259f16507cfa to your computer and use it in GitHub Desktop.
Save ksugar/db5eb3a97397092749db259f16507cfa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
from pathlib import Path
from timagetk.io import imread
from timagetk.io import imsave
def main(args):
pin = Path(args.input)
if pin.is_file():
if pin.suffix != ".inr" and set(pin.suffixes) != set(".inr", ".gz"):
raise ValueError("suffix should be one of .inr or .inr.gz")
filenames = [pin]
else:
filenames = list(pin.glob("*.inr")) + list(pin.glob("*.inr.gz"))
if args.output:
pout = Path(args.output)
pout.mkdir(parents=True, exist_ok=True)
else:
pout = pin.parent
for filename in filenames:
outfile = str(
pout / str(filename).replace(".gz", "").replace(".inr", ".tif")
)
imsave(outfile, imread(str(filename)))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert .inr or (.inr.gz) to .tif")
parser.add_argument("input",
type=str,
help="input file or directory")
parser.add_argument("-o", "--output",
type=str,
help="output directory")
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment