I have the following code:
#load in image
image = cv2.imread('lenna.png')
title = "foo"
ax = plt.axes([0,0,1,1])
ax.clear()
height, width = image.shape[:2]
ax.axis('off')
ax.set_title(title)
#some things plotted etc.
But then I need the figure as numpy array for further computation, so I am doing the following:
masked_image = image
ax.imshow(masked_image.astype(np.uint8),interpolation="nearest")
ax.figure.canvas.draw()
w,h = ax.figure.get_size_inches()*ax.figure.get_dpi()
I = np.fromstring(ax.figure.canvas.tostring_rgb(),dtype=np.uint8).reshape(int(h),int(w),3)
#Has the white borders around it
Image.fromarray(I)
However, I still has now white borders around it, is there an easy way to remove the white borders without saving the figure?
The image I used is the following:
which does not have any white borders around it
However after the code above, it looks like the following:

Which now has white bars around it.
Other already posted solution to this are all relying on saving the image, which I do not want

imageis a numpy array already. Why producing a plot and saving as numpy array again?