使用matplotlib和pyplot保存图像数组

时间:2017-12-19 16:51:19

标签: python-3.x matplotlib

我有一组输出图像,我一个接一个地显示运行时。我想将图像保存在名为'已保存图像的文件夹中。我制作的当前代码保存图像,一旦生成image1并保存在image1.png中,图像就不会单独保存.png image2会覆盖其位置,如何避免用新图像覆盖以前的图像

 for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  (boxes, scores, classes, num) = sess.run(
      [detection_boxes, detection_scores, detection_classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      np.squeeze(boxes),
      np.squeeze(classes).astype(np.int32),
      np.squeeze(scores),
      category_index,
      use_normalized_coordinates=True,
      line_thickness=8)
  plt.savefig('image1.png')
  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)

2 个答案:

答案 0 :(得分:1)

您的所有图片都保存为' image1.png'。而是使用变量将图像保存为不同的文件,如image1.png,image2.png,image3.png ......

#initiate a variable
i = 0
for image in TEST_ARRAY_IMAGES:
  <your code as above>
  # save as image1.png, image2.png
  plt.savefig('image'+str(i)+'.png')
  # increment i
  i += 1
  <any code here>

答案 1 :(得分:0)

这有效:

i=0
for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  (boxes, scores, classes, num) = sess.run(
      [detection_boxes, detection_scores, detection_classes, num_detections],
      feed_dict={image_tensor: image_np_expanded})
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      np.squeeze(boxes),
      np.squeeze(classes).astype(np.int32),
      np.squeeze(scores),
      category_index,
      use_normalized_coordinates=True,
      line_thickness=8)

  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)

  plt.savefig('image'+str(i)+'.png')

  i+=1