1

I just want to save the picture without the padding and the axis.
Below is the code:

#-*- 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()

Then I get: enter image description here

ManmeetP
  • 801
  • 7
  • 17
hey6775
  • 159
  • 3
  • 14
  • It would be great if you could submit as [minimal working example](https://stackoverflow.com/help/mcve) that we could run to check our solution and show its effect. – Tom de Geus Nov 22 '17 at 07:30
  • @TomdeGeus,OK,I submit to github.here:https://github.com/hei6775/example.git – hey6775 Nov 22 '17 at 07:39
  • That is still too complicated! You can use some random data to get rid of the 'SimpleITK' library. You question is really about matplotlib and imshow. – Tom de Geus Nov 22 '17 at 07:41
  • @TomdeGeusThat's right.I am tring to solve the problem – hey6775 Nov 22 '17 at 07:43
  • So please submit it here... Edit your question such that only uses numpy as matplotlib, then we can really help. – Tom de Geus Nov 22 '17 at 07:45

1 Answers1

1

took my answer from here: How to completely remove the white space around a scatterplot?

replace your last three lines with:

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()
Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42
  • I have read the article.But it seems to have no effect on me.Actually,the ax.axis('off') can delete the axis.The white padding still exists.I want to get 240*240 picture by cut the picture. – hey6775 Nov 23 '17 at 08:15