Search code examples
imagematplotlibimage-processingpython-imaging-libraryfigure

How can I save image without frame in python?


I am plotting image by wavelet coefficients but I don't know how to save it without frame. I tried to do this by plt.savefig but it didn't work.

Any help would be appreciated.

cA5,cD5,cD4,cD3,cD2,cD1=coeffs
for i, ci in enumerate(coeffs): 
        plt.imshow(ci.reshape(1, -1), extent=[0, 3844, i + 0.5, i + 1.5],cmap='inferno',aspect='auto',interpolation='nearest') 
        plt.ylim(0.5, len(coeffs) + 0.5) 
        plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])

Solution

  • I understand you want to fit your image. Use plt.axis('off') before plt.savefig

    For example, we have the following image.

    enter image description here

    We can use:

    • plt.axis('off') remove the axis
    • plt.savefig("test.png", bbox_inches='tight')

    Output:

    enter image description here

    Code


    scale = plt.imread(fname='27BR1.jpg')
    plt.axis('off')
    plt.imshow(scale, interpolation='nearest')
    plt.savefig("test.png", bbox_inches='tight')
    

    Possible Question: There is still a white area under the background?


    Answer: Then you can use opencv


    import cv2
    
    img = cv2.imread('27BR1.jpg')
    cv2.imwrite("test.png", img)
    

    Output:

    enter image description here