【问题标题】:Read new DICOM files in folder with pyinotify and pydicom [duplicate]使用 pyinotify 和 pydicom 读取文件夹中的新 DICOM 文件 [重复]
【发布时间】:2018-07-31 18:04:43
【问题描述】:

我正在尝试使用以下代码使用 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."

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

【问题讨论】:

  • 应用于您的问题的副本:使用mask = pyinotify.IN_CLOSE_WRITE重试

标签: python pydicom pyinotify


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多