使用pyinotify和pydicom读取文件夹中的新DICOM文件

时间:2018-07-31 18:04:43

标签: python dicom pydicom pyinotify

我正在尝试使用以下代码通过pyinotify看门狗处理给定文件夹中的新文件:

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()

并出现以下错误:

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

文件未损坏。错误中的第一个数字可能更大或更小,约为预期的30-90%(19633600)。似乎没有足够的时间来读取像素数据。

1 个答案:

答案 0 :(得分:1)

如果问题是您所说的(文件未损坏,并且文件仍在写入磁盘),则您想重命名方法以使用process_IN_CLOSE_WRITE

赞:

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()