Python matplotlib仅绘制具有所需分辨率和dpi的图像

Python matplotlib仅绘制具有所需分辨率和dpi的图像,python,matplotlib,collections,Python,Matplotlib,Collections,我已经阅读了stackoverflow关于这个主题的所有问题,但没有成功。 上面的代码是我能得到的最接近无轴无边框图像的代码。 我无法控制图像分辨率,因为在gimp中打开图像时的dpi与我选择的不同。 我想有一个7158 x 16392,72 dpi,没有任何边界或帧的最终图像 from stl import mesh from matplotlib import collections from matplotlib import pyplot figure, axes = pyplot.s

我已经阅读了stackoverflow关于这个主题的所有问题,但没有成功。 上面的代码是我能得到的最接近无轴无边框图像的代码。 我无法控制图像分辨率,因为在gimp中打开图像时的dpi与我选择的不同。 我想有一个7158 x 16392,72 dpi,没有任何边界或帧的最终图像

from stl import mesh
from matplotlib import collections
from matplotlib import pyplot

figure, axes = pyplot.subplots(frameon=False)
figure.set_size_inches(99.5,228)
# Read the STL file
your_mesh = mesh.Mesh.from_file('2d_35MeshTest.STL')

axes.set_xlim(your_mesh.min_[0], your_mesh.max_[0])
axes.set_ylim(your_mesh.min_[1], your_mesh.max_[1])

axes.add_collection(collections.PolyCollection(your_mesh.vectors[:, :, :2], linewidth=1, 
    facecolors=(1,1,1),alpha=1, edgecolors=(1,0,0) ))

print your_mesh.vectors.shape
pyplot.gca().set_aspect('equal')

pyplot.axis('off')
ax = pyplot.Axes(figure, [0., 0., 1., 1.], )
figure.set_dpi(72)

figure.subplots_adjust(bottom = 0)
figure.subplots_adjust(top = 1)
figure.subplots_adjust(right = 1)
figure.subplots_adjust(left = 0)

figure.savefig('image_refined8.png', format='png', transparent=True, bbox_inches='tight', pad_inches =  0.001)        
stl文件在这里 编辑后图像输出为

from stl import mesh
from matplotlib import collections
from matplotlib import pyplot

figure, axes = pyplot.subplots(frameon=False)
pixel_width = 7158  # ADDED
pixel_height = 16392  # ADDED
req_dpi = 72  # ADDED
figure.set_size_inches(pixel_width / float(req_dpi),  # MODIFIED
                       pixel_height / float(req_dpi))  # MODIFIED
# Read the STL file
your_mesh = mesh.Mesh.from_file('2d_35MeshTest.STL')

axes.set_xlim(your_mesh.min_[0], your_mesh.max_[0])
axes.set_ylim(your_mesh.min_[1], your_mesh.max_[1])

axes.add_collection(collections.PolyCollection(your_mesh.vectors[:, :, :2],
                                               linewidth=1,
                                               facecolors=(1,1,1),alpha=1,
                                               edgecolors=(1,0,0) ))

print your_mesh.vectors.shape
pyplot.gca().set_aspect('equal')

pyplot.axis('off')
ax = pyplot.Axes(figure, [0., 0., 1., 1.], )
figure.set_dpi(72)

figure.subplots_adjust(bottom = 0)
figure.subplots_adjust(top = 1)
figure.subplots_adjust(right = 1)
figure.subplots_adjust(left = 0)

figure.savefig('image_refined7.png', format='png',
               transparent=True, dpi=req_dpi)  # MODIFIED
这就产生了一个.png,对我来说是7158x16392像素,没有轴,并且(据我所知)没有丢失或额外的数据。
如前所述,您需要在savefig对话框(或其他地方的savefig.dpi)中定义
dpi
,如果您想要像素精确的图形,则不能使用
bbox='tight'
。如果您运行Python3或从uuu future\uuuuu导入分区运行

我希望能够选择分辨率,则不需要在
req\u dpi
周围浮动。我希望最终的图像有7158 x 16392与72 dpi,没有任何边界或帧。如果你看这张图片,你会看到一堆我不想要的透明像素。我误解了你的主要意图。只要数字是7158 x 16392像素,dpi问题就不存在。如果您希望发生这种情况,您不应该使用bbox_inches='tight',因为这会调整图形的大小。您还可以在关闭轴后添加轴。顺便说一句,图像对我来说是完全白色的,除了轴和透明边框(可能是pad_英寸)。我似乎也无法运行你的代码。
stl
来自哪里?我已经更新了代码和文件。当我使用“紧身”时,它不合适。