Skip to content
Snippets Groups Projects
Commit fefce4cb authored by Harry Carey's avatar Harry Carey
Browse files

added the ability to read dzip files

parent 3e311103
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,8 @@ import cv2 ...@@ -10,6 +10,8 @@ import cv2
from skimage import measure from skimage import measure
import threading import threading
import re import re
from .reconstruct_dzi import reconstruct_dzi
def number_sections(filenames, legacy=False): def number_sections(filenames, legacy=False):
""" """
...@@ -208,7 +210,10 @@ def segmentation_to_atlas_space( ...@@ -208,7 +210,10 @@ def segmentation_to_atlas_space(
): ):
"""Combines many functions to convert a segmentation to atlas space. It takes care """Combines many functions to convert a segmentation to atlas space. It takes care
of deformations.""" of deformations."""
segmentation = cv2.imread(segmentation_path) if segmentation_path.endswith(".dzip"):
segmentation = reconstruct_dzi(segmentation_path)
else:
segmentation = cv2.imread(segmentation_path)
if pixel_id == "auto": if pixel_id == "auto":
# Remove the background from the segmentation # Remove the background from the segmentation
segmentation_no_background = segmentation[~np.all(segmentation == 255, axis=2)] segmentation_no_background = segmentation[~np.all(segmentation == 255, axis=2)]
......
...@@ -8,6 +8,7 @@ from datetime import datetime ...@@ -8,6 +8,7 @@ from datetime import datetime
import os import os
class PyNutil: class PyNutil:
"""A utility class for working with brain atlases and segmentation data. """A utility class for working with brain atlases and segmentation data.
......
import cv2
import numpy as np
import os
import zipfile
import xmltodict
def reconstruct_dzi(zip_file_path):
"""
Reconstructs a Deep Zoom Image (DZI) from a zip file containing the tiles.
:param zip_file_path: Path to the zip file containing the tiles.
:return: The reconstructed DZI.
"""
with zipfile.ZipFile(zip_file_path, "r") as zip_file:
# Get the highest level of the pyramid
highest_level = str(
np.max(
[
int(os.path.split(os.path.split(i)[0])[1])
for i in zip_file.namelist()
if i.endswith(".png")
]
)
)
# Get the filenames of the highest level tiles
highest_level_files = [
i for i in zip_file.namelist() if i.endswith(".png") and i.split("/")[-2] == highest_level
]
# Read the DZI file
dzi_file = zip_file.open([i for i in zip_file.namelist() if i.endswith(".dzi")][0])
xml = dzi_file.read()
json_data = xmltodict.parse(xml)
tileSize = json_data["Image"]["@TileSize"]
width, height = int(json_data["Image"]["Size"]["@Width"]), int(json_data["Image"]["Size"]["@Height"])
# Create an empty image
image = np.zeros((height, width, 3), dtype=np.uint8)
# Fill in the image with the highest level tiles
for file in highest_level_files:
with zip_file.open(file) as f:
contents = f.read()
# Decode the binary PNG data
image_ = cv2.imdecode(np.frombuffer(contents, np.uint8), cv2.IMREAD_COLOR)
x, y = map(int, os.path.splitext(os.path.split(file)[1])[0].split("_"))
x, y = x * int(tileSize), y * int(tileSize)
image[y : y + image_.shape[0], x : x + image_.shape[1], :] = image_
# Save the image
return image
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment