Read new DICOM files in folder with pyinotify and pydicom

696 Views Asked by At

I'm trying to process new files in given folder with pyinotify watchdog with this code:

import pyinotify
import pydicom as pyd

class EventHandler(pyinotify.ProcessEvent):

    def process_IN_CREATE(self, event):
        if not event.dir:
            print("Got new file: ", event.pathname)
            img = pyd.dcmread(event.pathname)
            pix = img.pixel_array


wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE
loop = asyncio.get_event_loop()
notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=EventHandler())
wdd = wm.add_watch('/home/Dicom/work', mask, rec=True, auto_add=True)

try:
    loop.run_forever()
except:
    print('\nshutting down...')

loop.stop()
notifier.stop()

And got the following error:

AttributeError: "Amount of pixel data 5045478 does not match the expected data 19633600."

The file is not corrupted. The first number in the error may be greater or lower, about 30-90% of expected (19633600). It looks like it has not enough time to read pixel data.

1

There are 1 best solutions below

0
On

If the problem is what you say it is (that the file is not corrupted and that the file is still being written to disk) you want to rename your method to use the process_IN_CLOSE_WRITE instead.

Like this:

import pyinotify
import pydicom as pyd

class EventHandler(pyinotify.ProcessEvent):

    def process_IN_CLOSE_WRITE(self, event):
        if not event.dir:
            print("Got new file: ", event.pathname)
            img = pyd.dcmread(event.pathname)
            pix = img.pixel_array


wm = pyinotify.WatchManager()
mask = pyinotify.IN_CLOSE_WRITE
loop = asyncio.get_event_loop()
notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=EventHandler())
wdd = wm.add_watch('/home/Dicom/work', mask, rec=True, auto_add=True)

try:
    loop.run_forever()
except:
    print('\nshutting down...')

loop.stop()
notifier.stop()