【问题标题】:Module VTK is not able to read file in Python3 on Windows模块 VTK 无法在 Windows 上的 Python3 中读取文件
【发布时间】:2019-03-17 17:25:48
【问题描述】:

我想读取 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 模块,但我遇到了同样的问题。

这是一个错误吗?或者有什么办法可以解决吗?还是想法?

【问题讨论】:

    标签: python python-3.x io vtk


    【解决方案1】:

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

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

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

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

    所以要解决这个问题:

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

    • 使用 vtkStructuredPointsWriter 重写文件的新版本 但请记住将编写器设置为 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 版本:

    import platform
    import vtk
    
    print(platform.python_version())
    print(platform.python_version_tuple())
    print(vtk.vtkVersion.GetVTKSourceVersion())
    

    由于它适用于我的设置,我建议您仔细检查路径(将所有 f.ex. 放入 c:\temp 并测试它是否有效!)。

    【讨论】:

    • 本周末我将测试您的解决方案。但是您的解决方案的唯一问题是我在 Windows 上时无法转换文件,因为它是在 Windows 上生成的(由用 MinGW 编译的 fortran 代码生成,如果后者不模拟 linux,我会徘徊)。另一个奇怪的地方是,ParaView 能够从 linux 和 windows 读取 vtk 二进制文件。
    • 我使用您的脚本转换文件并在 ParaView 上检查:一切正常。然后我尝试使用Output_253_ASCII.vtk 重新运行我的脚本,我得到关于维度的相同错误。对不起。这似乎是个好主意。还有其他想法吗?
    • 我认为您的 Python+VTK 设置有问题。在我的(Windows,Python 3.6.5 VTK 8.1.2)上,它适用于二进制或 ASCII 文件,尺寸被正确读取(即 1000,1,1)。我想仔细检查一下我正在使用原始文件,但您指向数据文件的链接已过期。
    • 我更新问题以刷新链接。您可能对设置是正确的,但我该如何检查和纠正它?
    • 我发布了检查版本的代码,将其添加到脚本的开头。我正在运行 VTK 8.1.0,我更新到 8.1.2,它仍然有效。在任何情况下,您都可以尝试安装新版本的 Python 并“pip install vtk”。
    【解决方案2】:

    感谢 L.C. 的建议。我可以确定问题来自路径及其编码。实际上,它包含空格和重音。当将路径提供给 C++ vtkDataReader.cxx 函数时,这会导致错误的编码。

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

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多