| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #!/usr/bin/env python3
- # tifffile/tiffcomment.py
- """Print or replace ImageDescription in first page of TIFF file.
- Usage: ``tiffcomment [--set comment] file``
- """
- from __future__ import annotations
- import os
- import sys
- try:
- from .tifffile import tiffcomment
- except ImportError:
- try:
- from tifffile.tifffile import tiffcomment
- except ImportError:
- from tifffile import tiffcomment # noqa: PLW0406
- def main(argv: list[str] | None = None) -> int:
- """Tiffcomment command line usage main function."""
- comment: str | bytes | None
- if argv is None:
- argv = sys.argv
- if len(argv) > 2 and argv[1] in '--set':
- comment = argv[2]
- files = argv[3:]
- else:
- comment = None
- files = argv[1:]
- if len(files) == 0 or any(f.startswith('-') for f in files):
- print()
- print(__doc__.strip())
- return 0
- if comment is None:
- pass
- elif os.path.exists(comment):
- with open(comment, 'rb') as fh:
- comment = fh.read()
- else:
- try:
- comment = comment.encode('ascii')
- except UnicodeEncodeError as exc:
- print(f'{exc}')
- assert isinstance(comment, str)
- comment = comment.encode()
- for file in files:
- try:
- result = tiffcomment(file, comment)
- except Exception as exc:
- print(f'{file}: {exc}')
- else:
- if result:
- print(result)
- return 0
- if __name__ == '__main__':
- sys.exit(main())
|