-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
34 lines (24 loc) · 860 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import io
import matplotlib.pyplot as plt
import pydicom
from matplotlib.image import imread
from pydicom.filebase import DicomFileLike
def read_dicom_dataset(raw_data: bytes) -> pydicom.Dataset:
"""Permet de lire les bytes d'un fichier DICOM et le transformer en Dataset DICOM"""
buffer = io.BytesIO(raw_data)
ds = pydicom.dcmread(buffer)
return ds
def dicom_dataset_to_bytes(ds: pydicom.Dataset) -> io.BytesIO:
"""Permet d'écrire un Dataset DICOM en bytes"""
buffer = io.BytesIO()
memory_dataset = DicomFileLike(buffer)
pydicom.dcmwrite(memory_dataset, ds)
memory_dataset.seek(0)
return buffer
def show_image_from_bytes(raw_data: bytes) -> None:
buffer = io.BytesIO(raw_data)
img = imread(buffer, format='png')
plt.figure(figsize=(16, 12))
plt.imshow(img)
plt.axis('off')
plt.show()