|
|
import openslide |
|
|
|
|
|
def find_level_for_target_mpp(slide_path, target_mpp): |
|
|
""" |
|
|
Find the level in the slide that corresponds to the target MPP. |
|
|
|
|
|
Parameters: |
|
|
slide_path (str): Path to the slide file. |
|
|
target_mpp (float): Target microns per pixel (MPP). |
|
|
|
|
|
Returns: |
|
|
int: Level number that corresponds to the target MPP or None if not found. |
|
|
""" |
|
|
slide = openslide.OpenSlide(slide_path) |
|
|
|
|
|
print(slide.properties) |
|
|
|
|
|
|
|
|
x_resolution = float(slide.properties.get('tiff.XResolution')) |
|
|
y_resolution = float(slide.properties.get('tiff.YResolution')) |
|
|
resolution_unit = slide.properties.get('tiff.ResolutionUnit') |
|
|
|
|
|
|
|
|
if resolution_unit == 'centimeter': |
|
|
mpp_x = 10000 / x_resolution |
|
|
mpp_y = 10000 / y_resolution |
|
|
else: |
|
|
print("Resolution unit is not in centimeters. Adjust the calculation accordingly.") |
|
|
return None |
|
|
|
|
|
|
|
|
if not mpp_x or not mpp_y: |
|
|
print("Could not calculate MPP due to missing or invalid resolution information.") |
|
|
return None |
|
|
|
|
|
|
|
|
for level in range(slide.level_count): |
|
|
|
|
|
level_mpp_x = mpp_x * slide.level_downsamples[level] |
|
|
level_mpp_y = mpp_y * slide.level_downsamples[level] |
|
|
|
|
|
|
|
|
if abs(level_mpp_x - target_mpp) < 0.1 and abs(level_mpp_y - target_mpp) < 0.1: |
|
|
print(f"Level {level} corresponds to approximately {target_mpp} MPP.") |
|
|
return level |
|
|
|
|
|
print(f"No level corresponds to approximately {target_mpp} MPP.") |
|
|
return None |
|
|
|