[Python] Matplotlib save cropped Image with patches, not figure

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    I want to save a cropped picture from matplotlib. The cropped patch is a circle, so I want to have a picture in a circle saved to explorer.

    import matplotlib.pyplot as plt
    import matplotlib.cbook as cbook
    import matplotlib.patches as patches

    with cbook.get_sample_data('grace_hopper.jpg') as image_file:
    image = plt.imread(image_file)

    fig, ax = plt.subplots()
    im = ax.imshow(image)
    patch = patches.Circle((260, 200), radius=200, transform=ax.transData)
    im.set_clip_path(patch)

    ax.axis('off')
    plt.show()


    What I expect is:

    • the round patch with the picture in it, without some white space around the circe patch.

    • Outside the patch ther could also be a transperancy area.

    • What I get is this: [​IMG]

    Anyone any suggestions?

    Continue reading...

Compartilhe esta Página