VSCode - Display an image while debugging

Viewed 3122

I am currently using visual studio code to debug a python file. In that python file is a numpy image. When i try to import matplotlib to display the image, it doesn't show:

- plt.imshow(pred_scores)
<matplotlib.image.AxesImage object at 0x7f3d43e691d0>
- plt.show()
None
- pred_scores
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
...

Basically nothing happens. Is it actually possible to show an image here?

4 Answers

I'm not aware of any way of actually showing the image, but for debugging purposes, you could always just save it to a file using matplotlib.pyplot.savefig:

plt.savefig('debug.png')

Or just use this method.

from threading import Thread
import cv2

def showImage(img):

    def show(img):
        cv2.imshow(str(dt.datetime.today()), img)
        cv2.waitKey()

    Thread(target=show, args=[img]).start()

You could use VS Code Jupyter Interactive Windows to render your plots.

At the entry script of your solution code base (e.g. main.py), either start a debugging session in Interactive Window (> Jupyter: Debug Current File in Interactive Window) from the command palette, or, as I prefer, add #%% at the first line and click Debug Cell.

enter image description here

You can now interactively render your plots during a VS Code debugging session, regardless of whether it is locally or remotely

enter image description here

There is a discussion about this on GitHub

https://github.com/microsoft/vscode-jupyter/issues/1278

Related