【问题标题】:Is there a way to download images that I create with the matplotlib pyplot library in python? [duplicate]有没有办法下载我在 python 中使用 matplotlib pyplot 库创建的图像? [复制]
【发布时间】:2021-11-28 01:51:05
【问题描述】:

我觉得这可能是一个基本问题,但我需要为项目创建一个具有随机尺寸的椭圆数据库。我现在正在 Google Colab 中执行以下代码:

import numpy as np
import cv2
from matplotlib import pyplot as plt
import pandas as pd
from PIL import Image 

from skimage import color

for i in range(2):
  img = np.full((64,64,3), 255, dtype=np.uint8)
  center_x = np.random.randint(30,45)
  center_y = np.random.randint(30,45)
  major_axis = np.random.randint(1,9)
  minor_axis= np.random.randint(1,9)
  angle = 0
  B = 0
  G = 0
  R = 0
  ellipseCoords = [img, (center_x,center_y), (major_axis, minor_axis), angle, 0, 360, (B,G,R), -1]
  a = cv2.ellipse(img, (center_x, center_y), (major_axis, minor_axis), angle, 0, 360, (B, G, R), -1)
  plt.imshow(a)
  a.shape

上面的代码按预期创建并绘制了两个椭圆。最后一件事我最终需要大约 64-100,因此单独保存图像似乎非常耗时。更糟的是,我会这样做,但我更喜欢使用计算机化的方式。

这可能吗?我似乎找不到任何有用的东西...

谢谢!

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以在每次迭代结束时使用 matplotlib 的 savefig 函数 (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html),如下所示:

    for i in range(n):
        # generate elipse
    
        plt.imshow(a)
        plt.savefig(f'elipse_{i}.png')
    

    【讨论】:

    • 非常感谢!我一定会试一试的。
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2010-10-18
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    相关资源
    最近更新 更多