[Python] Save images to disk from array in python [duplicate]

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to also save the images in the array, I'm able to return them but not save them, I get AttributeError: 'Axes' object has no attribute 'array_interface'

    import random
    import matplotlib.animation as animation
    from IPython.display import HTML
    import matplotlib.pyplot as plt
    import PIL.Image

    # set seeds for the same images
    torch.manual_seed(0)
    random.seed(0)
    np.random.seed(0)

    def generate(text: str, n=3, stddev=0.2):

    encoded_text = torch.tensor(lang_encode(text)).unsqueeze(0).to(dev)
    latent_v = translation_model(encoded_text).squeeze(0)

    images = []
    for i in range(n):
    random_noise = i * stddev * torch.randn(*latent_v.shape)
    vector = latent_v + random_noise.to(dev)
    generated_image = G(vector.to(dev), None)
    images.append((parse_img(generated_image.cpu())).resize((2048, 2048), Image.LANCZOS))

    fig, axs = plt.subplots(*(1, n), figsize=(10, 10))
    for i, img in enumerate(images):
    axs.imshow(img.resize((2048, 2048)))
    axs.axis("off")
    PIL.Image.fromarray(axs).save(f'out/seed{i:04d}.png')
    return fig

    print('Enter search term:')
    searchterm = input()
    generate(searchterm, n=10, stddev=0.05)

    Continue reading...

Compartilhe esta Página