Show Pillow (PIL) Image in WSL Ubuntu

Aug 6, 2020

Show image via Pillow

from PIL import Imageim = Image.open("happy.jpg")im.show()

You need to install an image viewer and X server to display the image.

Install Image Viewer

sudo apt install imagemagick

Install X-Server

You need to install X Servers for Windows, using either VcXsrv or Xming. In only tested with Xming. When prompted for the firewall access, allow both private and public network (If you miss that, you can set it at Windows Security -> Firewall & network protection -> Allow an app through firewall. Launch Xming (you will see the app icon at bottom-right corner next to the clock, expand to see).

If you are WSL2, you need to run

export DISPLAY=`grep -oP "(?<=nameserver ).+" /etc/resolv.conf`:0.0

(getting the IP of Windows Host/X Server from /etc/resolv.conf). If you are on WSL1, export DISPLAY=localhost:0.0 will do. You might want to add this to ~/.bashrc.

Try to the code to show the image.

The request might be blocked/rejected by xming.

Check you Xming log (right click bottom-right Xming app icon and click View log and you should see

AUDIT: Tue Jul 28 18:31:11 2020: 20788 C:\Program Files (x86)\Xming\Xming.exe: client 4 rejected from IP 172.29.10.201

Edit C:\Program Files (x86)\Xming\X0.hosts (open with Administrator priviledge to enable edit/save). Add the rejected IP.

localhost
172.29.10.201

NOTE: Note that WSL Ubuntu IP is not static

Exit Xming server and start again.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.

Tags