-
Notifications
You must be signed in to change notification settings - Fork 9
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
from_tiff convenience function #51
Conversation
I got you fam: https://github.com/labsyspharm/ome-tiff-pyramid-upgrade/blob/master/pyramid_upgrade.py#L36 |
lol... I had to google that 😂 wow 😱 that looks amazing. I think I'm all for it if you are! Though I do want a bit more thorough background on exactly what the difference are. what the limitations of tifffile are, what motivated you to do this, what potential limitations might be here... but yeah. this looks crazy |
For now, if we just want to extract the ImageDescription it's probably 10-20 lines of code. I can omit all the defensive code (which I needed since I didn't want to corrupt files on write), writing code, and general purpose Tag/TagSet data structures. I'll see what I can scrape together. PR against this branch? The rest of the code in TiffSurgeon would only be required for a full OME-TIFF read/write library, which is out of scope for ome_types. Or is it...? |
yeah, I'd like to see what that looks like ... (also feel free to open a new PR if you want)
To me, it really doesn't matter if it's in |
Here's a small standalone function to read the first ImageDescription tag value (as NULL terminated bytes) from an open TIFF file handle: def tiff_image_description(fh):
"""Return value of first ImageDescription tag from open TIFF file."""
from struct import unpack
offsetsize, offsetformat, tagnosize, tagnoformat, tagsize, codeformat = {
b'II*\0': (4, '<I', 2, '<H', 12, '<H'),
b'MM\0*': (4, '>I', 2, '>H', 12, '>H'),
b'II+\0': (8, '<Q', 8, '<Q', 20, '<H'),
b'MM\0+': (8, '>Q', 8, '>Q', 20, '>H'),
}[fh.read(4)]
fh.read(4 if offsetsize == 8 else 0)
fh.seek(unpack(offsetformat, fh.read(offsetsize))[0])
for _ in range(unpack(tagnoformat, fh.read(tagnosize))[0]):
tagstruct = fh.read(tagsize)
if unpack(codeformat, tagstruct[:2])[0] == 270:
size = unpack(offsetformat, tagstruct[4:4+offsetsize])[0]
if size <= offsetsize:
return tagstruct[4+offsetsize:4+offsetsize+size]
fh.seek(unpack(offsetformat, tagstruct[-offsetsize:])[0])
return fh.read(size) See also https://docs.openmicroscopy.org/ome-model/6.1.2/ome-tiff/code.html#extracting-a-tiff-comment |
Resolves #50 |
Thanks @cgohlke! That's about what I was envisioning. :) |
Thanks @cgohlke! |
Also ignore Flake E203 since Black imposes some patterns that are incompatible with it (and Black forces formatting for all cases that E203 covers, anyway). Co-authored-by: Christoph Gohlke <cgohlke@uci.edu> Co-authored-by: Christoph Gohlke <cgohlke@uci.edu>
adds a
from_tiff
convenience to generate anOME
class from a tiff, usingtifffile
.Stopped short of adding
tifffile
to dependencies though (added an extra). Ideas for extracting XML withouttifffile
welcome.