VTK模块无法在Windows上读取Python3中的文件

2024-09-28 13:14:45 发布

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

我想读VTK文件做一些处理。 因为我必须在Linux和Windows上进行这种处理,所以用Python3来处理就更容易了。 因此,Linux和Windows都有Python3(3.6.0)及其模块VTK(版本8.1.2)。在

我创建MWE是为了突出问题:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from vtk import *
import sys
import os


if __name__ == "__main__":
    pathFile1 = os.getcwd()+'/Output_253.vtk'
    print(pathFile1)

    if os.path.exists(pathFile1):

        # Creation of variables with the right type to read STRUCTURES_POINTS VTK files
        readerVTK1 = vtk.vtkStructuredPointsReader()

        # We put the content of our files in our variables
        readerVTK1.SetFileName(pathFile1)
        readerVTK1.Update()

        # We read our variables datas, hence we have our VTK files datas in these variables
        dataVTK1 = readerVTK1.GetOutput()

        # We check if the dimensions are not zeros
        if dataVTK1.GetDimensions()!=(0,0,0):
            (dimX,dimY,dimZ) = dataVTK1.GetDimensions()
            print((dimX,dimY,dimZ))
        else :
            print('dimensions are null... Problem !')
    else:
        print(' [WARN]   ','the file you are looking for do not exist')
        print(' pathFile1: ', pathFile1 )

脚本中引用的文件Output_253.vtk可以通过链接下载:here

然后,当我在Linux上运行这个脚本时,我得到了'(1000,1,1)',它与文件头和我的其余处理相一致。在Windows上我得到'dimensions are null... Problem !'。在

我试图在Windows上重新安装VTK模块,但是我遇到了同样的问题。在

那是虫子吗?或者有办法解决吗?或者想法?在


Tags: 文件theimportifoslinuxwindowsour
2条回答

看看vtkStructuredPointsWriter类,在文档中它说:

警告在一个系统上写入的二进制文件在其他系统上可能无法读取。

这可能就是问题的原因(在文本编辑器中编辑文件,它是二进制的):

https://vtk.org/doc/nightly/html/classvtkStructuredPointsWriter.html

所以要解决这个问题:

  • 在Linux中读取该文件(它似乎可以工作)

  • 使用vtkStructuredPointsWriter重新写入文件的新版本 但请记住将writer设置为ASCII模式(通过调用SetFileTypeToASCII()

例如,可以使用以下python脚本将其转换为ASCII:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from vtk import *
import sys
import os


if __name__ == "__main__":
    pathFile1 = os.getcwd()+'/Output_253.vtk'
    print(pathFile1)

    if os.path.exists(pathFile1):

        # Creation of variables with the right type to read STRUCTURES_POINTS VTK files
        readerVTK1 = vtk.vtkStructuredPointsReader()

        # We put the content of our files in our variables
        readerVTK1.SetFileName(pathFile1)
        readerVTK1.Update()

        # We read our variables datas, hence we have our VTK files datas in these variables
        dataVTK1 = readerVTK1.GetOutput()

        pathFile2 = os.getcwd()+'/Output_253_ASCII.vtk'
        writer = vtk.vtkStructuredPointsWriter()
        writer.SetFileName(pathFile2)
        writer.SetFileTypeToASCII()
        writer.SetInputData(dataVTK1)
        writer.Write()

    else:
        print(' [WARN]   ','the file you are looking for do not exist')
        print(' pathFile1: ', pathFile1 )

使用以下代码运行脚本时,可以检查正在使用的Python和VTK版本:

^{pr2}$

当它在我的设置中工作时,我建议您仔细检查路径(将所有f.ex.都放在c:\temp中,并测试它是否有效!)。在

多亏了L.C.的建议,我可以确定问题来自路径及其编码。实际上,它包含空格和重音。当路径被赋予C++ ^ {CD1}}函数时,这会导致错误的编码。在

一个简单的解决方法是将目录更改为包含该文件的文件夹或最后一个不带重音符号的文件夹。在本例中,只需将路径的定义改为pathFile1='./Output_253.vtk',问题就迎刃而解了。在

相关问题 更多 >

    热门问题