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()
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
- I installed tinker
- I launched the container with .Xauthority && /tmp/.X11-unix/X0
- I tried almost all the answers of
- Almost every answer is for os with desktop, not for docker
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
- add the python code and run it with: python main.py