[英]How to convert vtk to stl in python?
I have around 500 vtk files that need to be converted to stl file. 我有大约500个需要转换为stl文件的vtk文件。 I usually use Paraview to convert them manually, but it takes forever.
我通常使用Paraview对其进行手动转换,但这需要花很长时间。 I wonder if there is a tool to convert vtk files into stl files in Python?
我想知道是否有工具可以在Python中将vtk文件转换为stl文件吗? I attached a screenshot of my VTK file.
我附上了我的VTK文件的屏幕截图。
Thanks in advance. 提前致谢。
How about something like the following? 如下所示呢? Save the code as
file_converter.py
and run the script with arguments: 将代码另存为
file_converter.py
并使用参数运行脚本:
python file_converter.py -h
python file_converter.py <path-to-input-dir> -o <path-to-output-dir>
And here the code for the converter. 这里是转换器的代码。
#!/usr/bin/env python
import os
import vtk
import argparse
def convertFile(filepath, outdir):
if not os.path.isdir(outdir):
os.makedirs(outdir)
if os.path.isfile(filepath):
basename = os.path.basename(filepath)
print("Copying file:", basename)
basename = os.path.splitext(basename)[0]
outfile = os.path.join(outdir, basename+".stl")
reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(filepath)
reader.Update()
writer = vtk.vtkSTLWriter()
writer.SetInputConnection(reader.GetOutputPort())
writer.SetFileName(outfile)
return writer.Write()==1
return False
def convertFiles(indir, outdir):
files = os.listdir(indir)
files = [ os.path.join(indir,f) for f in files if f.endswith('.vtk') ]
ret = 0
print("In:", indir)
print("Out:", outdir)
for f in files:
ret += convertFile(f, outdir)
print("Successfully converted %d out of %d files." % (ret, len(files)))
def run(args):
convertFiles(args.indir, args.outdir)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="VTK to STL converter")
parser.add_argument('indir', help="Path to input directory.")
parser.add_argument('--outdir', '-o', default='output', help="Path to output directory.")
parser.set_defaults(func=run)
args = parser.parse_args()
ret = args.func(args)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.