有没有办法在Python中使用matplotlib pyplot库创建的图像下载图像?

18 浏览
0 Comments

有没有办法在Python中使用matplotlib pyplot库创建的图像下载图像?

这个问题已经有答案了:

使用Matplotlib将绘图保存为图像文件而不是显示

我觉得这可能是一个基本问题,但我需要为项目创建一个具有随机维度的椭圆数据库。 我现在在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个,因此单独保存图像似乎非常耗时。 最坏的情况是,我将这样做,但我更喜欢使用计算机化的方式。

这种做法可能吗? 我似乎找不到任何有用的方法...

谢谢!

admin 更改状态以发布 2023年5月24日
0
0 Comments

您可以在每次迭代结束时使用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')

0