Skip to main content
  1. Posts/

Display Image with Pillow inside Ubuntu on Windows

··268 words·2 mins·
Table of Contents

With the help of Windows Subsystem for Linux (WSL, for short)1, we can use the nearly full Linux environment on Windows system. In this post, I would like to share how to display images inside Ubuntu on Windows, which is one of the popular distrubtions.

If you try to show an image with Pillow using the following code inside WSL:

from PIL import Image

im = Image.open("test.jpg")

im.show()

you will find that nothing happens if you run the above code. The reason is two-fold:

  1. There is no valid image viewer on your Ubuntu on Windows (Pillow will use the system default image viewer, which is Imagemagick on Linux)
  2. There is no X server by which you can use GUI applications.

After knowing the reasons, the solution is simple. First, we need to install Imagemagick for Ubuntu on Windows (not on Windows 10 itself, this is important!):

sudo apt install imagemagick

Then we need to install a X server on the Windows system, which will serve your GUI applications. A good option for Windows is Vcxsrv. You can install it manually or with the help of Chocolatey:

choco install vcxsrv

After install, run it on your system. Inside Ubuntu on Windows, you should also set the DISPALY environment variable in your .bashrc:

echo "export DISPLAY=localhost:0.0" >> ~/.bashrc && source ~/.bashrc

Run your Python code again and you should be able to see the image displayed properly on your screen.

References
#


  1. See here on how to install a Linux distribution with WSL enabled. ↩︎

Related

Ubuntu on Windows Missing after Windows Update
··283 words·2 mins
What Is The Difference between pip, pip3 and pip3.6 Shipped with Anaconda3?
··222 words·2 mins
How to Use shutil.move() on Windows and Linux
··331 words·2 mins