【问题标题】:How to delete the padding in the picture? [duplicate]如何删除图片中的填充? [复制]
【发布时间】:2017-11-22 07:13:43
【问题描述】:

我只是想保存没有填充和轴的图片。
下面是代码:

#-*- coding:utf-8 -*-
import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt

url = r"G:to\my\filename.mha"
path = r'F:\image\s.png'
image = sitk.ReadImage(url)

max_index = image.GetDepth()
max_width = image.GetWidth()
max_height = image.GetHeight()
print(max_index,max_width,max_height)


# As list of 2D numpy arrays which cannot be modified (no data copied)
xxx = sitk.GetArrayViewFromImage(image)[75,:,:]
zzz = plt.imshow(xxx,cmap = 'gray')
plt.savefig(path)
plt.show()

然后我得到:

【问题讨论】:

  • 如果您能以minimal working example 的身份提交我们可以运行以检查我们的解决方案并显示其效果,那就太好了。
  • @TomdeGeus,好的,我提交给 github.here:github.com/hei6775/example.git
  • 那还是太复杂了!您可以使用一些随机数据来摆脱“SimpleITK”库。你的问题真的是关于 matplotlib 和 imshow。
  • @TomdeGeus没错。我正在努力解决问题
  • 所以请在这里提交...编辑你的问题,只使用numpy作为matplotlib,那么我们真的可以提供帮助。

标签: matplotlib


【解决方案1】:

从这里得到我的答案:How to completely remove the white space around a scatterplot?

将最后三行替换为:

fig,ax = plt.subplots(1)

fig.subplots_adjust(left=0,right=1,bottom=0,top=1)

ax.imshow(xxx)

ax.axis('tight')
ax.axis('off')
plt.savefig(path)
plt.show()

【讨论】:

  • 这篇文章我看过了。但是好像对我没有任何影响。其实ax.axis('off')可以删除轴。白色的padding还是存在的。我想get 240*240图片通过剪切图片。
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 2020-01-03
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
相关资源
最近更新 更多