"Pillow was built without XCB support"

Question:

I’m working on a program that uses ImageGrab in Pillow. I am getting the error mentioned in the title. I notice in the documentation that it says the generic pip install Pillow doesn’t come with libxcb. I tried installing libxcb with pip install libxcb, but it apparently doesn’t exist as that. I tried looking around on Google for it, but none of it helped.

If anybody could point me to the specific library that I need to install and commands to run, I’d appreciate it!

I should mention that the python that I’m running is the Windows Store v3.8. I am trying to keep a minimal amount on my SSD and didn’t want a large overhead of stuff I won’t use.

Asked By: abyssmu

||

Answers:

I finally figured it out. What was going on is that I was trying to use grab(x, y, w, h) without the bbox=(x, y, w, h) parameter. Over my two day journey, I did not find a single helpful thing on the Internet. I thought the whole time it was not working because of a missing package or some Linux/Windows conversion dependency.

I hope this helps anybody that comes across this very simple, but agonizing error.

Here is exactly what I was doing:

def grab(x, y, w, h):
    screen = np.array(ImageGrab.grab(x, y, w, h)) # Throws XCB error
    ...
    return screen

Here is the correct code for a Windows platform:

def grab(x, y, w, h):
    screen = np.array(ImageGrab.grab(bbox=(x, y, w, h))) # Throws no errors
    # screen = np.array(ImageGrab.grab()) # Alternative that grabs full screen
    ...
    return screen
Answered By: abyssmu

I had the same error while I was using the Ubuntu terminal for Windows 10. Figured that was the issue because it sometimes has weird errors when its executing more os orientated stuff.

If anyone else has this error and they’re using the Ubuntu terminal, try running it with the Windows CMD.

Answered By: Koala__Chips

you can use ‘pyscreenshot’ python package that works just like PIL.ImageGrab

import pyscreenshot as ImageGrab
bbox=(0, 0, 1366, 768)
screenshot = ImageGrab.grab()

https://pypi.org/project/pyscreenshot/

Answered By: ffletcherr

I had this error using Pillow v8.1.2 installed from conda. I fixed it by replacing the conda version with a pip version:

conda remove Pillow
pip install Pillow
Answered By: Leopd

You can fix this issue by running

pip install --upgrade Pillow

The new version of Pillow (i.e, 8.3.2) include all optional libraries except libimagequant for windows, mac and Linux, and libxcb for windows.
To know more, you can refer to this official link

Answered By: kriptonian

I am new to Pythoning and also faced the same problem while trying to use a script written in Windows in Ubuntu. In my case, the answer with using bbox= didn’t work, so I started to look for another solution. According to the documentation: "On Linux, if xdisplay is None then gnome-screenshot will be used if it is installed". So I simply installed the gnome-screenshot and my code now working even without specifying the bbox.

Answered By: Rustem Nizamov