Too many changes :(

This commit is contained in:
Ross
2025-07-29 09:20:58 +01:00
parent ad4be1e9a6
commit 4460545442
20 changed files with 361 additions and 81 deletions
+41
View File
@@ -2,6 +2,7 @@ import base64
from collections import defaultdict
import hashlib
import mimetypes
from pydicom.uid import ExplicitVRLittleEndian
from io import BytesIO
@@ -19,6 +20,7 @@ try:
from .pydicom_PIL import get_PIL_image
except:
from pydicom_PIL import get_PIL_image
import numpy as np
def image_as_base64(image_file):
"""
@@ -284,3 +286,42 @@ def compare_dicom_datasets(*datasets: pydicom.Dataset):
return differences
def combine_dicom_images_side_by_side(dicom_path1, dicom_path2):
"""
Combines two DICOM images side by side, using the metadata from the first image.
If the heights differ, pads the shorter image with zeros at the bottom.
Returns a new pydicom.Dataset with the combined image.
"""
ds1 = pydicom.dcmread(dicom_path1)
ds2 = pydicom.dcmread(dicom_path2)
arr1 = ds1.pixel_array
arr2 = ds2.pixel_array
h1, w1 = arr1.shape[:2]
h2, w2 = arr2.shape[:2]
# Pad arrays if heights differ
if h1 != h2:
max_h = max(h1, h2)
def pad(arr, target_h):
pad_height = target_h - arr.shape[0]
if pad_height > 0:
pad_shape = ((0, pad_height), (0, 0)) if arr.ndim == 2 else ((0, pad_height), (0, 0), (0, 0))
arr = np.pad(arr, pad_shape, mode='constant', constant_values=0)
return arr
arr1 = pad(arr1, max_h)
arr2 = pad(arr2, max_h)
combined = np.hstack((arr1, arr2))
ds1.PixelData = combined.tobytes()
ds1.Rows, ds1.Columns = combined.shape[:2]
if hasattr(ds1, 'PhotometricInterpretation'):
ds1.PhotometricInterpretation = ds1.PhotometricInterpretation
# Ensure output is uncompressed
ds1.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian
return ds1