python opencv with docker: Matplotlib.show Matplotlib is currently using agg, which is a non-GUI backend
NickName:JRichardsz Ask DateTime:2021-12-20T10:11:37

python opencv with docker: Matplotlib.show Matplotlib is currently using agg, which is a non-GUI backend

I need to run opencv + python in docker.

This code should show a GUI popup with an image:

import cv2
from matplotlib import pyplot as plt
img = cv2.imread("image.jpg")
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()

image

It works fine on a ubuntu desktop but the problem is with docker. I got this error:

UserWarning: Matplotlib is currently using agg, 
which is a non-GUI backend, so cannot show the figure.
  plt.show()

Following some post, if I add this

import matplotlib
matplotlib.use('TkAgg')

I got another similar error:

ImportError: Cannot load backend 'TkAgg' which 
requires the 'tk' interactive framework, as 
'headless' is currently running

attempts

workaround

I know that runs GUI applications is not the goal of docker, but I want to use docker to have my operative system clean while I developing on python.

If I change the show() by plt.savefig("mygraph.png") , the image is created instead the gui popup.

steps to replicate the issue with ubuntu

  • docker run
docker run -it  \
--workdir=/app \
-v /your/workspace:/app \
-v /tmp/.X11-unix/X0:$XSOCK -e DISPLAY=0  \
jjanzic/docker-python3-opencv  bash
  • install matplotlib
pip install matplotlib

Copyright Notice:Content Author:「JRichardsz」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70416767/python-opencv-with-docker-matplotlib-show-matplotlib-is-currently-using-agg-wh

More about “python opencv with docker: Matplotlib.show Matplotlib is currently using agg, which is a non-GUI backend” related questions

python opencv with docker: Matplotlib.show Matplotlib is currently using agg, which is a non-GUI backend

I need to run opencv + python in docker. This code should show a GUI popup with an image: import cv2 from matplotlib import pyplot as plt img = cv2.imread("image.jpg") img_gray = cv2.cvtC...

Show Detail

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. how to solve it

Show Detail

In a source code, I typed it in and got: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure

The code is import numpy as np import matplotlib.pyplot as plt import sys image = cv2.imread(sys.argv[1]) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(gray, cmap="gray") plt.

Show Detail

matplotlib pyplot UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure

I have some problems related to plt.show() and I would describe the problem and what I tried. #Problem I am using python and plt.show() and those worked well until 4 hours before. However, when I d...

Show Detail

PyCharm Matplotlib "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. plt.show()"

I am having problems with matplotlib.pyplot.show() function. I am using PyCharm on Linux, and i have a VirtualEnv. When i execute the file x.py in the built terminal in PyCharm (using venv) like th...

Show Detail

py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. % get_backend())

I was trying to run the Mask-RCNN repository provided by the matterport in Github. https://github.com/matterport/Mask_RCNN. when I run the demo in the anaconda, it showed "C:\Anaconda\lib\site-pac...

Show Detail

matplotlib using non-GUI backend even though tkinter is installed and imported

I'm using the Jupyter notebook extension in VS Code. I'm using Camelot to extract tables from a pdf, and I'm trying to do some visual debugging to find the coordinates of the column separators. When

Show Detail

Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend problem

I am interested to know how ginput() of matplotlib works?When I run the following simple code axis([-1, 1, -1, 1]) print( "Please click three times") pts = ginput(3) I get :3: UserWarning:

Show Detail

Python Matplotlib: How to use non-gui and gui backend in one program?

Hey I spend a good amount at google but couldn't find a solution for this: I'm plotting a graph embededded in a pygame window using a non-gui-backend: import matplotlib as mpl mpl.use("Agg") imp...

Show Detail

UserWarning: Matplotlib is currently using agg, so cannot show the figure

I'm trying to run a basic matplotlib example from the official website: However, when i run the code, my Python interpreter complains and outputs the following message: UserWarning: Matplotlib is

Show Detail