gbook / nidb

NeuroInformatics Database
GNU General Public License v3.0
26 stars 8 forks source link

Missing thumb.png #117

Closed andersonwinkler closed 1 year ago

andersonwinkler commented 1 year ago

For many old sequences, the thumb.png file is missing.

Is there a preferred call to ImageMagick to generate it? (or preferred options to be passed to "convert")

Thanks!

gbook commented 1 year ago

NiDB attempts to create a thumb.png first using the Nifti file created during QA (moduleMRIQA)

slicer {niftifile} {path}/thumb.png

QA will occasionally not generate a nifti file (if QA fails to run for example), so a fallback is to get it from the DICOM file

convert -normalize {dicomfile} {path}/thumb.png

convert comes from ImageMagick, which is generally good at reading DICOM images. Really old images like ACR/NEMA may not be readable by ImageMagick.

andersonwinkler commented 1 year ago

Thank you!

This helped find that the issue is that convert won't work for some of the older DICOMs. If others have a similar issue, the script below will generate the thumb.png file for the missing ones:

import os
import glob
import pydicom
import numpy as np
import png

Subjects = sorted([os.path.split(x)[-1] for x in glob.glob(os.path.join('/nidb','data','archive','*'))])
for subj in Subjects:
    dicomdirs = sorted(glob.glob(os.path.join('/nidb','data','archive',subj,'*','*','dicom')), key=len)
    for d in dicomdirs:
        if os.path.exists(os.path.join(os.path.split(d)[0],'thumb.png')):
            print('Skipping {}'.format(d))
        else:
            print('Generating thumb for {}'.format(d))
            try:
                dcmlist = sorted(glob.glob(os.path.join(d,'*.dcm')))
                midfile = dcmlist[int(len(dcmlist)/2)]
                ds  = pydicom.dcmread(midfile)
                img = ds.pixel_array
                mi  = np.quantile(img, .02,  axis=None)
                ma  = np.quantile(img, .99,  axis=None)
                img[img>ma] = ma
                img[img<mi] = mi
                img = pydicom.pixel_data_handlers.util.apply_voi_lut(img, ds)
                img = np.uint8((img - np.min(img)) / np.max(img) * 255.0)
                with open(os.path.join(os.path.split(d)[0], 'thumb.png'), 'wb') as fid:
                    w = png.Writer(img.shape[1], img.shape[0], greyscale=True)
                    w.write(fid, img)
            except:
                pass
    print("")