Skip to content

Commit

Permalink
add pillow to requirements.txt, show basic metadata of patient at the…
Browse files Browse the repository at this point in the history
… top left of each DICOM image
  • Loading branch information
yjg30737 committed Nov 13, 2023
1 parent 933a4ad commit f77fd63
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from dicomImageView import DicomImageViewer
from findPathWidget import FindPathWidget
from script import get_tiled_dicom_images_in_directory
from script import get_dicom_images_in_dir

# Get the absolute path of the current script file
script_path = os.path.abspath(__file__)
Expand Down Expand Up @@ -66,7 +66,7 @@ def __initUi(self):

def __addToList(self, dirname):
self.__listWidget.clear()
self.__dicom_arr = get_tiled_dicom_images_in_directory(dirname)
self.__dicom_arr = get_dicom_images_in_dir(dirname)
filenames = [_['filename'] for _ in self.__dicom_arr]
self.__listWidget.addItems(filenames)
f = self.__listWidget.count() > 0
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PyQt5>=5.14
pydicom
pydicom
pillow
39 changes: 30 additions & 9 deletions script.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import numpy as np
import pydicom
import os
from PIL import Image, ImageDraw, ImageFont


def tile_dicom_image(dicom_file):
def get_dicom_image(dicom_file):
# Read the DICOM file
ds = pydicom.dcmread(dicom_file)
ds.PatientName = ''
ds.PatientID = ''

dicom_dict = {}
dicom_dict['data'] = []
Expand All @@ -21,20 +24,38 @@ def tile_dicom_image(dicom_file):
if dd.keyword in key_prop:
dicom_dict[dd.keyword] = dd.repval[1:-1]

print(dicom_dict)

dicom_dict_arr = []
# Extract the pixel data
image_data = ds.pixel_array
for tile in image_data:
w, h = tile.shape
for d in image_data:
w, h = d.shape
dicom_dict['width'] = w
dicom_dict['height'] = h
dicom_dict['data'].append(tile[0])
# dicom_dict['data'].append(d[0])

if 'RescaleIntercept' in ds and 'RescaleSlope' in ds:
d = d * ds.RescaleSlope + ds.RescaleIntercept
d = np.uint8(d)
pil_image = Image.fromarray(d)
draw = ImageDraw.Draw(pil_image)
font = ImageFont.load_default()
text = f'''
StudyDate: {ds.StudyDate}
PatientName: {ds.PatientName}
PatientID: {ds.PatientID}
PatientBirthDate: {ds.PatientBirthDate}
PatientSex: {ds.PatientSex}
'''
draw.text((10, 10), text, fill=255, font=font)
dicom_dict['data'].append(np.array(pil_image))

return dicom_dict

def get_tiled_dicom_images_in_directory(dirname):
ts_arr = []
def get_dicom_images_in_dir(dirname):
ds_arr = []
for filename in os.listdir(dirname):
filename = os.path.join(dirname, filename)
ts_arr.append(tile_dicom_image(filename))
return ts_arr
ds_arr.append(get_dicom_image(filename))
return ds_arr

0 comments on commit f77fd63

Please sign in to comment.