DigitalSlideArchive / tifftools

Pure Python tools for reading and writing all TIFF IFDs, sub-IFDs, and tags.
Other
49 stars 5 forks source link

Split and Merge by Python #95

Open alexlsa opened 3 months ago

alexlsa commented 3 months ago

Hi,

Thanks for this project

Is it possible to run split and merge with python interface without using the filesystem ?

manthey commented 3 months ago

When you do tifftools.read_tiff(<buffer or path>) it returns a data structure that includes a list of the ifds (directories). split stores each directory as its own file; merge takes a list of ifds from a list of files are writes out the result.

You can reference individual ifds, and create a new list and then save it. For instance, this merges the first two ifds from a first source with the last two ifds from a second source. The sources and destinations can be paths or streams or io buffers. Depending on the actual contents of the sources, the program may use temp files as part of its processing.

info1 = tifftools.read_tiff(<source 1>)
info1['ifds']  # this is a list
info2 = tifftools.read_tiff(<source 2>)
mergedifds = info1['ifds'][:2] + info2['ifds'][-2:]
tifftools.write_tiff(merged_ifds, <destination>)
alexlsa commented 3 months ago

Thank you @manthey, it worked very well. Now I can remove any level (ifds) from source file

One small question : Is it possible to delete ifds without reading whole source file ? I mean, I do not want to read and write source file, because file is very big I just want to remove specific ifds from generic tif. All operations will be done on a single source file

manthey commented 3 months ago

tifftools won't do that -- it isn't within its design intent. But you can modify a tiff file to skip ifds. An ifd has a structure of

<location of next ifd or 0 to indicate last ifd>, so you could rewrite the 4 or 8 bytes of the location of the ifd you want to remove at the end of the preceding ifd to point to the next ifd.

alexlsa commented 3 months ago

Thank you,

Can I do this with tifftools ? or maybe python tifffile ?