Home > Mobile >  Jupyter shows an image / plot, but saves a blank file
Jupyter shows an image / plot, but saves a blank file

Time:11-25

I am reading an image and saving it. However, the saved image is a blank. I am running the following code to create the plot

# Create plot
path = i[1][0][0]
img = np.array(Image.open(path))
plt.figure()
fig, ax = plt.subplots(1)
ax.imshow(img)

Image created

This creates the above plot. Then I run the following commands to save this image. Before saving I want to see the image. However , it generates a blank image. What could be the issue ? I am using Jupyter notebook and have run the command %matplotlib inline as well.

# Save generated image with detections
plt.axis("off")
plt.gca().xaxis.set_major_locator(NullLocator())
plt.gca().yaxis.set_major_locator(NullLocator())
filename = os.path.basename(path).split(".")[0]
output_path = os.path.join("output2", filename ".png")
plt.savefig(output_path, bbox_inches="tight", pad_inches=0.0)
plt.show()
plt.close()

This generates a blank image. Blank Image

CodePudding user response:

  • All of the code related to the plot (e.g axes and figure) must be in the same cell, not multiple cells.
  • As indicated in this enter image description here

    • Raw image for testing

    enter image description here

  • Related