Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement generic date tag resetting #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 56 additions & 19 deletions bin/reset_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@
import tiffsurgeon


# Define a custom argument type for a list of integers
def list_of_ints(arg):
return list(map(int, arg.split(",")))


def parse_args():
parser = argparse.ArgumentParser(
description="Set all DateTime tags in a TIFF to one value (in place).",
description="Set all specified date tags in a TIFF to one value (in place).",
)
parser.add_argument("image_path", help="TIFF file to process")
parser.add_argument(
"datetime",
"--datetags",
help="Space-separated list of tags (integer ids) with dates to replace (default: 306)",
nargs="+",
type=int,
default=[306],
)
parser.add_argument(
"--datetime",
help="YYYY:MM:DD HH:MM:SS string to replace all DateTime values."
"Note the use of colons in the date, as required by the TIFF standard.",
" Note the use of colons in the date, as required by the TIFF standard."
" Default: 1970:01:01 00:00:00 (start of Unix epoch).",
type=str,
default="1970:01:01 00:00:00",
)
argv = sys.argv[1:]
# Allow date and time to be passed as separate args for convenience.
Expand All @@ -35,6 +50,17 @@ def main():
tiff = tiffsurgeon.TiffSurgeon(
args.image_path, encoding="utf-8", writeable=True
)
tiff.read_ifds()
except (tiffsurgeon.FormatError, UnicodeDecodeError) as e:
print(f"TIFF format or encoding error: {e}. Trying with Latin-1 encoding.")
try:
tiff = tiffsurgeon.TiffSurgeon(
args.image_path, encoding="latin-1", writeable=True
)
tiff.read_ifds()
except tiffsurgeon.FormatError as e:
print(f"Error reading IFDs: {e}")
sys.exit(1)
except tiffsurgeon.FormatError as e:
print(f"TIFF format error: {e}")
sys.exit(1)
Expand All @@ -48,22 +74,33 @@ def main():

tiff.read_ifds()

subifds = [
tiff.read_ifd(v) for i in tiff.ifds if 330 in i.tags for v in i.tags[330].value
]
offsets = [
i.tags[306].offset_range.start for i in tiff.ifds + subifds if 306 in i.tags
]
if offsets:
for x in offsets:
tiff.file.seek(x)
tiff.file.write(new_datetime)
print(
f"Successfully replaced {len(offsets)} DateTime values in"
f" {args.image_path}"
)
else:
print(f"No DateTime tags found in {args.image_path} -- file not modified")
for tag in args.datetags:
if not any(tag in i.tags for i in tiff.ifds):
print(f"Tag {tag} not found in {args.image_path} -- file not modified")
sys.exit(1)

subifds = [
tiff.read_ifd(v)
for i in tiff.ifds
if 330 in i.tags
for v in i.tags[330].value
]
offsets = [
i.tags[tag].offset_range.start for i in tiff.ifds + subifds if tag in i.tags
]

if offsets:
for x in offsets:
tiff.file.seek(x)
tiff.file.write(new_datetime)
print(
f"Successfully replaced {len(offsets)} values of tag {tag} in"
f" {args.image_path}"
)
else:
print(
f"No tags with number {tag} found in {args.image_path} -- file not modified"
)

tiff.close()

Expand Down
4 changes: 4 additions & 0 deletions main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ params.rm_svs_macro = false // Remove macro image in SVS files
params.rm_svs_label = true // Remove label image in SVS files
params.rm_ome_sa = true // Remove structural annotations in OME-XML files

// Tags that contain a DateTime to be reset
// Ideally we would parse as a list but this works for now
params.datetags = "306 45278 45279 45494"

if (params.input) {
params.input = file(params.input)
} else {
Expand Down
2 changes: 1 addition & 1 deletion modules/reset_datetime.nf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ process reset_datetime {
"""
script:
"""
reset_datetime.py $image "1970:01:01 00:00:00"
reset_datetime.py $image --datetags $params.datetags
"""
}