【问题标题】:matplotlib draw only the image with desired resolution and dpimatplotlib 仅绘制具有所需分辨率和 dpi 的图像
【发布时间】:2016-06-07 13:10:21
【问题描述】:

我已阅读有关此主题的所有 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文件在这里stl file 图片输出为image output

【问题讨论】:

  • 我误解了你的主要意图。只要图形为 7158 x 16392 像素,则不存在 dpi 问题。如果您希望发生这种情况,则不应使用 bbox_inches='tight',因为这会调整图形的大小。关闭轴后,您还将添加轴。顺便说一句,图像对我来说完全是白色的,除了轴和透明边框(可能是 pad_inches)。我似乎也无法运行您的代码。 stl 来自哪里?
  • 我已经更新了代码和文件。当我使用“紧身”时,它不合适。

标签: python matplotlib collections


【解决方案1】:

修改后:

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

这会为我生成一个 7158x16392 像素的 .png,没有轴,并且(据我所知),没有丢失或附加数据。 正如我之前所说,您需要在 savefig-dialog(或 savefig.dpi 其他地方)中定义dpi,如果您想要像素精确的数字,则不能使用bbox='tight'。如果您运行 Python3 或 from __future__ import division,则不需要 req_dpi 周围的浮点数。

【讨论】:

  • 我希望能够选择分辨率。我希望最终图像具有 7158 x 16392 和 72 dpi,没有任何边框或框架。如果您查看图像,您会看到一列我不想要的透明像素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多