在Python中导入3D STL图像(导入错误:没有名为ascii的模块)

问题描述 投票:0回答:3

我计划用Python为raspberrypi编写一个程序,以导入3D STL图像。

为此,我在谷歌上搜索并找到了一个名为“numpy-stl”的Python库,它适合我的要求。我是按照网站的说明安装的

sudo pip install numpy-stl

然后尝试运行示例中给定的代码

from stl import mesh

# Using an existing stl file:
mesh = mesh.Mesh.from_file('some_file.stl')

# Or creating a new mesh:
VERTICE_COUNT = 100
data = numpy.zeros(VERTICE_COUNT, dtype=Mesh.dtype)
mesh = mesh.Mesh(data, remove_empty_areas=False)

# The mesh normals (calculated automatically)
mesh.normals
# The mesh vectors
mesh.v0, mesh.v1, mesh.v2
# Accessing individual points (concatenation of v0, v1 and v2 in triplets)
mesh.points[0] == mesh.v0[0]
mesh.points[1] == mesh.v1[0]
mesh.points[2] == mesh.v2[0]
mesh.points[3] == mesh.v0[1]

mesh.save('new_stl_file.stl')

但现在我面临以下错误:

Traceback (most recent call last):
  File "/home/pi/checkstl.py", line 1, in <module>
    from stl import stl
  File "/usr/local/lib/python2.7/dist-packages/stl/__init__.py", line 2, in <module>
    import stl.ascii
ImportError: No module named ascii 

任何人都可以指导我如何解决此错误吗? 谢谢你

python-2.7 numpy import 3d numpy-stl
3个回答
4
投票

更新 numpy-stl 后应该可以解决这个问题。 更重要的是,删除任何其他

stl
包 - 否则你会与模块名称发生冲突。 (numpy-stl 包作为
import stl
导入。)

如果安装了软件包stl 0.0.3,请先卸载它:

pip uninstall stl

然后,一旦安装了包 numpy-stl 就应该按预期工作(即可以通过

import stl
使用它):

pip install numpy-stl

0
投票

FWIW,您可以对 meshio (我编写的)执行相同的操作,只不过它适用于整个网格格式。

pip install meshio
import meshio

mesh = meshio.read("input.stl")  # or .off, .vtk, .ply, ...
# mesh.points, mesh.cells, ...

0
投票

我刚刚发布了最快、最轻、最直观的处理 STL 文件的库。这就是所谓的

openstl
。查看针对 meshio、numpy-stl 等的性能基准此处

pip install openstl
import openstl
import numpy as np

# Read
quad = openstl.read("quad.stl")

# Write
success = openstl.write("quad.stl", quad, openstl.format.binary) # Or openstl.format.ascii (slower but human readable)

# Rotate
rotation_matrix = np.array([
    [0,-1, 0],
    [1, 0, 0],
    [0, 0, 1]
])
rotated_quad = np.matmul(rotation_matrix, quad.reshape(-1,3).T).T.reshape(-1,4,3)

# Translate
translation_vector = np.array([1,1,1])
quad[:,1:4,:] += translation_vector # Avoid translating normals

# Scaling
scale = 1000.0
quad[:,1:4,:] *= scale # Avoid scaling normals

# Convert triangles to vertices and faces
vertices, faces = openstl.convert.verticesandfaces(triangles)
© www.soinside.com 2019 - 2024. All rights reserved.