Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

python - How to use matplotlib plots: removing axis, legends and white spaces without imshow?

I have been trying to save my image without the axis, white spaces, legends etc... without using imshow, this is because I don't want the image to/ try to appear in another window. Instead, I would like it to just save as image.png.

This is what I have tried so far but both try to show the image in another window which I don't want. But it does succeed in saving the image with no axis etc...

viz here is my data as it were.

viz = display_images(outputs.copy()) #(outputs.copy(), inputs.copy())

I've tried:

fig = plt.figure(frameon=False)
fig.set_size_inches(6.4,4.8)

ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)

ax.imshow(viz, aspect='auto')
fig.savefig('image.png')

To make this more clear, I have included the code for display_images although it may not be required to help with the initial issue.

def display_images(outputs, inputs=None, gt=None, is_colormap=True, is_rescale=True):
    import matplotlib.pyplot as plt
    import skimage
    from skimage.transform import resize

    plasma = plt.get_cmap('plasma')

    shape = (outputs[0].shape[0], outputs[0].shape[1], 3)
    
    all_images = []

    for i in range(outputs.shape[0]):
        imgs = []
        
        if isinstance(inputs, (list, tuple, np.ndarray)):
            x = to_multichannel(inputs[i])
            x = resize(x, shape, preserve_range=True, mode='reflect', anti_aliasing=True )
            imgs.append(x)

        if isinstance(gt, (list, tuple, np.ndarray)):
            x = to_multichannel(gt[i])
            x = resize(x, shape, preserve_range=True, mode='reflect', anti_aliasing=True )
            imgs.append(x)

        if is_colormap:
            rescaled = outputs[i][:,:,0]
            if is_rescale:
                rescaled = rescaled - np.min(rescaled)
                rescaled = rescaled / np.max(rescaled)
            imgs.append(plasma(rescaled)[:,:,:3])
        else:
            imgs.append(to_multichannel(outputs[i]))

        img_set = np.hstack(imgs)
        all_images.append(img_set)

    all_images = np.stack(all_images)
    
    return skimage.util.montage(all_images, multichannel=True, fill=(0,0,0))
    
    return skimage.util.montage(all_images, multichannel=True, fill=(0,0,0))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To sum up the comments: assuming it does not necessarily have to be matplotlib and you are saving array ranging from 0 to 1, you can do the following:

from PIL import Image
im = Image.fromarray((viz * 255).astype(np.uint8))
im.save('image.png')

After you added last block of code in your question, I can see you are using scikit-image, so you can as well just save your array with it:

skimage.io.imsave('image.png', (viz * 255).astype(np.uint8))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...