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

标签 python python-3.x io vtk

我想读取 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 上,我收到 “维度为空...问题!”

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

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

最佳答案

看看 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 并测试它是否有效!)。

关于python - 模块 VTK 无法在 Windows 上读取 Python3 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55209859/

相关文章:

python - Python 中池的使用

python - 从最多 6 位数字的字符串中提取数值,可选 2 位小数

python-3.x - "no module named torch"。但是在 Ubuntu 18.04.02 服务器版中安装了带有 conda 的 pytorch 1.3.0

python - 为什么我的灰度图像比彩色图像大

multithreading - 如果任何线程有异常,则导致 python 退出

python - 在散点图中标记点

python - 如何从声音中获取 Pi-Phase 以在 Python 中获得破坏性干扰

java - 如何将 HTTP 请求正文写入 Java Web 应用程序中的文件?

java - 如何关闭 IO?

c++ - 某些文件 I/O C++ 库函数的 Delphi 替代品?