I'm using OpenSlide and PIL to open slides and resize them. For some of the slides, the resulting images are partly black. To reproduce the error, use this slide and this script:
def slide_to_scaled_pil_image(slide, SCALE_FACTOR=32):
"""
Convert a WSI training slide to a scaled-down PIL image.
Args:
slide: An OpenSlide object.
Returns:
Tuple consisting of scaled-down PIL image, original width, original height, new width, and new height.
"""
large_w, large_h = slide.dimensions
new_w = math.floor(large_w / SCALE_FACTOR)
new_h = math.floor(large_h / SCALE_FACTOR)
level = slide.get_best_level_for_downsample(SCALE_FACTOR)
whole_slide_image = slide.read_region((0, 0), level, slide.level_dimensions[level])
whole_slide_image = whole_slide_image.convert("RGB")
img = whole_slide_image.resize((new_w, new_h), PIL.Image.BILINEAR)
return img, large_w, large_h, new_w, new_h
img = openslide.OpenSlide(PATH_TO_SLIDE)
ds_img, large_w, large_h, new_w, new_h = slide_to_scaled_pil_image(img, SCALE_FACTOR=SCALE_FACTOR)
So if I use different SCALE_FACTOR
I end up with a different result (ds_img
). SCALE_FACTOR=32
produces this:
while SCALE_FACTOR=64
produces this:
What is the problem here? Is there a way to overcome this issue if I need to use SCALE_FACTOR=32
?