我想阅读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模块,但我遇到了同样的问题。
那是一个错误吗?或者有什么方法可以解决?还是想法?
看一下vtkStructuredPointsWriter类,它在文档中说:
警告在一个系统上写入的二进制文件可能无法在其他系统上读取。
这可能是您的问题的原因(在文本编辑器中编辑您的文件,它是二进制文件):
https://vtk.org/doc/nightly/html/classvtkStructuredPointsWriter.html
所以解决这个问题:
例如,您可以使用此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中并测试它是否有效!)。
感谢L.C.的建议。我可以确定问题来自路径及其编码。实际上,它包含空格和重音。当路径被赋予C ++ vtkDataReader.cxx
函数时,这会导致错误的编码。
一个简单的解决方法是将目录更改为包含文件的文件夹或不带重音的最后一个文件夹。在目前的情况下,只需将路径的定义更改为pathFile1='./Output_253.vtk'
,问题就会得到解决。