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

2024-05-16 19:53:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用pyinotify watchdog处理给定文件夹中的新文件,代码如下:

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

得到以下错误:

^{pr2}$

文件未损坏。误差中的第一个数字可能更大或更小,大约是预期值(19633600)的30-90%。看起来它没有足够的时间读取像素数据。在


Tags: 文件inimportloopeventimgcreatemask
1条回答
网友
1楼 · 发布于 2024-05-16 19:53:33

如果问题是您所说的(文件没有损坏并且文件仍在写入磁盘),您需要重命名您的方法以使用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()

相关问题 更多 >