Skip to content

Commit 1f67fa6

Browse files
docs: add troubleshooting guide for OpenCV GUI backend errors
1 parent 4924e6b commit 1f67fa6

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

docs/gui-backend-issues.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Troubleshooting OpenCV GUI Backend Errors
2+
### (`cv2.imshow`, `libGL.so.1`, HighGUI backend not available)
3+
4+
Many Python users encounter GUI-related errors when using OpenCV functions such as
5+
`cv2.imshow()`, `cv2.namedWindow()`, or `cv2.waitKey()`.
6+
These errors often appear in **headless environments** such as:
7+
8+
- CI/CD pipelines
9+
- Docker containers
10+
- Cloud servers
11+
- WSL
12+
- Google Colab
13+
- SSH sessions
14+
15+
This guide explains:
16+
17+
- Common GUI error messages
18+
- Why these errors occur
19+
- How to fix them on Linux, Windows, Docker, CI/CD, WSL
20+
- When to use `opencv-python` vs. `opencv-python-headless`
21+
22+
---
23+
24+
## 1. Common Error Messages
25+
26+
```
27+
ImportError: libGL.so.1: cannot open shared object file
28+
cv2.error: (-2:Unspecified error) The function is not implemented
29+
error: (-215:Assertion failed) size.width>0 in function 'imshow'
30+
HighGUI backend error: No available GUI backend
31+
```
32+
33+
These errors occur because OpenCV cannot access the required **GUI backend** for creating display windows.
34+
35+
---
36+
37+
## 2. Why GUI Errors Occur
38+
39+
OpenCV uses **HighGUI** for display functions (`imshow`, `waitKey`, etc.).
40+
These depend on external system libraries such as:
41+
42+
- `libGL` (OpenGL)
43+
- GTK or Qt
44+
- X11 display server
45+
- Cocoa (macOS)
46+
- Windows native GUI APIs
47+
48+
Headless systems **do not include** these libraries, causing GUI errors when you attempt display operations.
49+
50+
Common environments without GUI support:
51+
52+
- GitHub/GitLab CI
53+
- Docker containers
54+
- Linux servers
55+
- Google Colab
56+
- WSL (Windows Subsystem for Linux)
57+
- SSH-only sessions
58+
59+
---
60+
61+
## 3. Fixes for Linux Desktop
62+
63+
If you are on a regular Linux machine **with GUI**, install missing dependencies:
64+
65+
```bash
66+
sudo apt update
67+
sudo apt install -y libgl1-mesa-glx libglib2.0-0
68+
sudo apt install -y libgtk2.0-dev libgtk-3-dev
69+
```
70+
## 4. Fixes for Windows
71+
72+
Most GUI issues on Windows come from incomplete or corrupted installations.
73+
Reinstall OpenCV:
74+
```bash
75+
pip install --upgrade opencv-python
76+
pip install --upgrade opencv-contrib-python
77+
```
78+
⚠️ Important:
79+
Do NOT install opencv-python-headless on Windows if you want GUI support.
80+
81+
## 5. Fixes for Headless Environments(CI, Docker, Colab, Servers)
82+
83+
In headless environments, **cv2.imshow()** will never work.
84+
85+
Install the headless version instead:
86+
```bash
87+
pip install opencv-python-headless
88+
```
89+
Use file-based output instead of GUI display:
90+
```python
91+
import cv2
92+
img = cv2.imread("image.jpg")
93+
cv2.imwrite("output.jpg", img)
94+
```
95+
## 6. Fix for Docker
96+
97+
Add GUI dependencies to your Dockerfile:
98+
```dockerfile
99+
RUN apt-get update && \
100+
apt-get install -y libgl1-mesa-glx libglib2.0-0
101+
```
102+
For headless images:
103+
```bash
104+
pip install opencv-python-headless
105+
106+
## 7. Fix For CI/CD(GitHub Actions)
107+
108+
```
109+
- name: Install OpenCV GUI dependencies
110+
run: |
111+
sudo apt-get update
112+
sudo apt-get install -y libgl1-mesa-glx libglib2.0-0
113+
```
114+

0 commit comments

Comments
 (0)