gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:13 24 4
gpt4 key购买 nike

我想读取 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com