Skip to content

Comments

Add function to get scale/units from vanilla tiff tags#63

Open
psobolewskiPhD wants to merge 2 commits intonapari:mainfrom
psobolewskiPhD:tiff_tag_units
Open

Add function to get scale/units from vanilla tiff tags#63
psobolewskiPhD wants to merge 2 commits intonapari:mainfrom
psobolewskiPhD:tiff_tag_units

Conversation

@psobolewskiPhD
Copy link
Member

@psobolewskiPhD psobolewskiPhD commented Oct 29, 2025

This PR uses tifffile API to get the x, y resolution (and the resolution unit) and convert to micron per pixel.
Tifffile is reading the tiff tags for this.

This was motivated by NDPI whole slide images (WSI), is based on looking at:
https://openslide.org/formats/hamamatsu/
https://www.loc.gov/preservation/digital/formats/content/tiff_tags.shtml
and tifffile API.
It should work for any tiff that uses tiff tags according to spec.

Added a set of simple tests that write tiffs with defined tiff tags for X,YResolution and ResolutionUnit and then check that we get the expected napari scale and units.

Edit: in practice, for ndpi, this PR benefits from #45

@codecov
Copy link

codecov bot commented Oct 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.18%. Comparing base (dd417b3) to head (236a715).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #63      +/-   ##
==========================================
+ Coverage   81.87%   83.18%   +1.31%     
==========================================
  Files           8        8              
  Lines         513      553      +40     
==========================================
+ Hits          420      460      +40     
  Misses         93       93              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TimMonko
Copy link
Contributor

Thanks @psobolewskiPhD, I think this would be worth adding a test for. From past experience tifffile made changes that resulted in different reading of units (I think it used to be inches and now its meters by default)

@psobolewskiPhD
Copy link
Member Author

As I noted in the OP, I'm not opposed to tests, I just don't have a reasonable test file.
I'll try to find some time to look into writing one using tifffile.

@psobolewskiPhD
Copy link
Member Author

Added a set of simple tests that write tiffs with defined tiff tags for X,YResolution and ResolutionUnit and then check that we get the expected napari scale and units.

Comment on lines +102 to +110
# XResolution = 100
# YResolution = 200
"resolution_unit, expected_units, expected_scale",
[
(1, ("pixel", "pixel"), (1 / 200, 1 / 100)),
# ResolutionUnit 2 = inches
(2, ("µm", "µm"), (25400 / 200, 25400 / 100)),
# ResolutionUnit 3 = cm
(3, ("µm", "µm"), (10000 / 200, 10000 / 100)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clear enough to me, and since not many people are reading the tests I think its ok. Just probably not clear to most people why 25400 might be used or something, but TBH I think its good enough

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want me to spell it out more, that X,YResolution are in pixels per Unit (inch or cm) and we need microns per pixel?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants