2

I hear that WSL2 has support for graphical programs.

I tried to run this on my ubuntu WSL installation (from rosetta code)

from PIL import Image
img = Image.new("RGB", (320, 240))
pixels = img.load()
pixels[100, 100] = (255, 0, 0)
img.show()

but my computer just beeps at me.

I have tried searching online for this but I keep getting articles from 2018 about setting up a 2018 X server or something.

Please help, Thank you

Glubs
  • 29

2 Answers2

0

Check out Ubuntu on Windows Community Preview and Microsoft/WSLg for latest updates on this topic. Note that WSLg can run single apps but not a full distro under WSL2 (yet), except you install a separate Xserver and Client for that.

UweG
  • 1
0

WSL2 with the latest Windows patches (particularly KB5020030) should have fixed many graphics problems, but your example code, while working fine on native Ubuntu, typically fails under WSL2. The ImageShow needs an image viewer from the system, perhaps you need to install one, like eog.

sudo apt install eog

Now you should see a blank window with a complaint about not being able to find some random named file like /tmp/tmp3fg12rgsf

This is a result of the eog viewer code section in ImageShow.py removing the file before the viewer can display it. Other viewers have a 20 second delay (or some local ping to delay), but not eog. What you say, the image will only display 20 seconds? Yes, show() is typically only used for debugging... enough said.

You can introduce you own delay in the Python code for the eog viewer: In file /lib/python3/dist-packages/PIL/ImageShow.py, line 243, in the eog viewer section, after the "Popen" and before the "remove" add:

  import time;time.sleep(20)

Careful to match the indenting, that's critical in python.

Terrible as it is to modify system code, ImageShow needs fixes for some display viewers. This fix is just for the eog viewer, display will have its own issues, and if you add the ImageMagick display, it will take precedence over eog, and crash, with some missing str.

This code just turned out to be a terrible test of WSL2 graphics.

ubfan1
  • 17,838