Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"X get_image failed: error 8 (73, 0, 924)" when trying to execute a script #5130

Closed
miathegaymer opened this issue Dec 24, 2020 · 29 comments
Closed

Comments

@miathegaymer
Copy link

miathegaymer commented Dec 24, 2020

What did you do?

I executed a script

What did you expect to happen?

A bot to run so it would play the Chrome Dinosaur Game for me.

What actually happened?

X get_image failed: error 8 (73, 0, 924)

What are your OS, Python and Pillow versions?

  • OS: Linux Command-Line VM on Chrome OS
  • Python: Python 3.8.5
  • Pillow: 8.0.1
import pyautogui
import time
import PIL.ImageGrab as ImageGrab
pyautogui.FAILSAFE = True

box_area1 = (248, 206, 286, 250) #box coordinates(x1,y1, x2,y2) to detect obstacles
pixel_plant1 = (19, 9) #pixel coordinates(x1,y1) to detect plants
pixel_plant2 = (11, 21) #pixel coordinates(x1,y1) to detect plants
pixel_plant3 = (29, 15) #pixel coordinates(x1,y1) to detect plants
pixel_plant4 = (20, 33) #pixel coordinates(x1,y1) to detect plants
pixel_plant5 = (20, 42) #pixel coordinates(x1,y1) to detect plants

def jump():
    pyautogui.keyDown('space')
    time.sleep(0.05)
    pyautogui.keyUp('space')
    print('Jump')
play = True
while play:
    try:
        box = ImageGrab.grab(box_area1)
        p1 = box.getpixel(pixel_plant1)
        p2 = box.getpixel(pixel_plant2)
        p3 = box.getpixel(pixel_plant3)
        p4 = box.getpixel(pixel_plant4)
        p5 = box.getpixel(pixel_plant5)
        if p1 == (83, 83, 83) or p2 == (83, 83, 83) or p3 == (83, 83, 83) or p4 == (83, 83, 83) or p5 == (83, 83, 83):
            jump()
            time.sleep(0.06)
    except Exception as e:
        play = False
        print(e)

This is not my script, I don't know Python. I do, however, know that this is an issue with the Pillow Library and that's why I'm bringing it here and not the script owner's GitHub repo.

@radarhere radarhere changed the title "X get_image failed: error 8 (73, 0, 924)" when trying to execute a script. "X get_image failed: error 8 (73, 0, 924)" when trying to execute a script Dec 24, 2020
@radarhere
Copy link
Member

Hi.

Yes, it is a Pillow error message. ImageGrab.grab would be triggering

PyErr_Format(PyExc_OSError, "X get_image failed: error %i (%i, %i, %i)",

My guess is that the solution involves passing an xdisplay argument for ImageGrab.grab.

Not sure how familiar you are with X, but if you open up your command line and run echo $DISPLAY, a value should be printed. What is that value? Could you try taking that value, and inserting into your code? Changing
box = ImageGrab.grab(box_area1)
to
box = ImageGrab.grab(box_area1, xdisplay="<INSERT $DISPLAY VALUE HERE>")

@miathegaymer
Copy link
Author

The output from the command line when I ran that returned with :0

Don't know if I should use that, or...

@miathegaymer
Copy link
Author

Okay, update. I changed the code to
box = ImageGrab.grab(box_area1, xdisplay="0")
And got a return in the command line of X connection failed: error 5

Still have no clue what any of it means.

@radarhere
Copy link
Member

box = ImageGrab.grab(box_area1, xdisplay=":0") - include the semicolon.

So, the bot is trying to take screenshots. That is the method it is using to determine where objects are in the game. X is the name of a windowing system for Linux.

My suggestion was that maybe it's trying to take a screenshot of the wrong display.

@nulano
Copy link
Contributor

nulano commented Dec 24, 2020

The error codes are from the X server or library. I can only find the list at http://www.rahul.net/kenton/xproto/xevents_errors.html, but you can see that, for example, error 5 is a parse error (caused by the omitted semicolon).

Based on that list, error 8 is a bad parameter, but more information is hard to find. What are your screen settings (resolution, bit depth)? Since you mention you are running in a "Linux Command-Line VM", do you even have a screen (in the VM)?

@radarhere
Copy link
Member

I don't think there's enough information here about the user's setup to solve this. It is entirely possible that the problem is that there isn't a display attached to the environment.

So closing due to lack of response. If you have more information, comment and this can be re-opened.

@MikeKall
Copy link

MikeKall commented Jun 24, 2021

Hi,

I am getting a similar error OSError: X get_image failed: error 8 (73, 0, 1336)
The script that I am running is very simple for now:

from PIL import ImageGrab

printscreen = ImageGrab.grab(xdisplay=":0")

Environment

Fedora 33
Screen resolution: 1920x1080
If it's useful, here is the device model I am running on: HP EliteBook 840 G7 Notebook PC

Any help would be appreciated

@radarhere
Copy link
Member

The error code is 8. After looking at https://stackoverflow.com/questions/56562823/xcb-get-image-reply-fails-for-window-on-another-workspace-desktop, it seems to be saying that Pillow is trying to screenshot something not on the screen.

...do you have multiple monitors attached?

@MikeKall
Copy link

No I have only one monitor

@radarhere
Copy link
Member

What happens if you run just ImageGrab.grab(), without the xdisplay argument?

@MikeKall
Copy link

I get the same error

@radarhere radarhere reopened this Jun 25, 2021
@radarhere
Copy link
Member

If you open up your command line and run echo $DISPLAY, what is the output?

@MikeKall
Copy link

It shows :0

@radarhere
Copy link
Member

https://stackoverflow.com/a/42447160 suggests that one needs to map the window first.

So I've thrown in xcb_map_window(connection, screen->root); - did you want to download and install from https://github.com/radarhere/Pillow/tree/xcb and see if that works for you?

@MikeKall
Copy link

MikeKall commented Jun 27, 2021

Ok, I am trying to install the library from the xcb branch but when I execute the setup.py it returns this error:
The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html

Traceback (most recent call last):
  File "/home/mikerose/project/capt/Pillow/setup.py", line 976, in <module>
    setup(
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib64/python3.9/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib64/python3.9/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python3.9/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/command/install.py", line 67, in run
    self.do_egg_install()
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/command/install.py", line 109, in do_egg_install
    self.run_command('bdist_egg')
  File "/usr/lib64/python3.9/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib64/python3.9/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/command/bdist_egg.py", line 164, in run
    cmd = self.call_command('install_lib', warn_dir=0)
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/command/bdist_egg.py", line 150, in call_command
    self.run_command(cmdname)
  File "/usr/lib64/python3.9/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib64/python3.9/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/command/install_lib.py", line 11, in run
    self.build()
  File "/usr/lib64/python3.9/distutils/command/install_lib.py", line 107, in build
    self.run_command('build_ext')
  File "/usr/lib64/python3.9/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib64/python3.9/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/mikerose/project/capt/lib64/python3.9/site-packages/setuptools/command/build_ext.py", line 79, in run
    _build_ext.run(self)
  File "/usr/lib64/python3.9/distutils/command/build_ext.py", line 340, in run
    self.build_extensions()
  File "/home/mikerose/project/capt/Pillow/setup.py", line 788, in build_extensions
    raise RequiredDependencyException(f)
__main__.RequiredDependencyException: jpeg

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/mikerose/project/capt/Pillow/setup.py", line 1033, in <module>
    raise RequiredDependencyException(msg)
__main__.RequiredDependencyException: 

The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html

I am not sure that I install it correctly, I haven't installed manually a module for python before.

@radarhere
Copy link
Member

radarhere commented Jun 27, 2021

That's a common error when compiling Pillow from source - you are, as it says, missing a jpeg dependency.

https://pillow.readthedocs.io/en/stable/installation.html#building-on-windows

If you wish to compile Pillow manually, you can use the build scripts in the winbuild directory used for CI testing and development. These scripts require Visual Studio 2017 or newer and NASM.

@MikeKall
Copy link

MikeKall commented Jun 27, 2021

Thanks I compiled it, but I get:
NameError: name 'xcb_map_window' is not defined

@radarhere
Copy link
Member

I've pushed a commit to the same branch to #include <xcb/xproto.h>. Does it work now?

@radarhere
Copy link
Member

I was unable to reproduce this in Ubuntu 20.04 in VirtualBox on my macOS machine.

Fedora 33 is now EOL. So I tried Fedora 35 in VirtualBox... and I could reproduce it!
After some Googling, I found xwd -root -display :0 as a command to simply take a screenshot. And I received BadMatch again. So I suspect there is just something going wrong between Fedora, X and VirtualBox. Or maybe just even Fedora and X, judging from https://stackoverflow.com/questions/68297504/major-opcode-of-failed-request-73-x-getimage-in-fedora-32-and-fedora-34.

So I'm unable to find any evidence that this is a problem with Pillow itself.

@vpcano
Copy link

vpcano commented May 7, 2022

I got the same error on Debian 11, and found that in my case it was caused by Gnome using Wayland and not Xorg. Logging in using Gnome on Xorg solved the problem.
Please notice that newest Linux distros come with Wayland enabled by default, so you may be getting this error because of the same reason as me.
Nevertheless, it would be great for Pillow to support Wayland...

@nulano
Copy link
Contributor

nulano commented May 7, 2022

Is there a built-in tool to take screenshots that works on Wayland? It might be worth looking at its source to compare differences.

@asweigart
Copy link

asweigart commented May 11, 2022

I get the same error running Ubuntu 21.10 running on VMware Worksation 15 Player on my Windows 10 laptop.

The Ubuntu VMware is running Python 3.9.7, with $DISPLAY set to ":0" and is simulting a single monitor. I installed Pillow 8.0.0.

I got the error running this from the command line:

>>> from PIL import ImageGrab
>>> ImageGrab.grab()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/al/.local/share/virtualenvs/deleteme-kZADQq03/lib/python3.9/site-packages/PIL/ImageGrab.py", line 64, in grab
    size, data = Image.core.grabscreen_x11(xdisplay)
OSError: X get_image failed: error 8 (73, 0, 335)

I get the same error when running the latest Pillow 9.1.0 too (except the error happens on line 68 of ImageGrab.py)

As a note, Ubuntu has used Wayland by default since 21.04. When getting screenshots with the mss library, I get the error "mss.exception.ScreenShotError: XGetImage() failed" which makes me think this might be an X-versus-Wayland issue.

@asweigart
Copy link

This is definitely an issue of Pillow not working on Wayland. I made one change in my Ubuntu 21.10 vm: modifying /etc/gdm3/custom.conf to uncomment #WaylandEnable=false and then ran sudo systemctl restart gdm3 to restart it so Ubuntu is using X instead of Wayland. Both ImageGrab.grab() and the mss module's screenshot function worked perfectly.

@radarhere
Copy link
Member

The question of Wayland now has a dedicated issue - #6312

@AmoghSaxena
Copy link

This is definitely an issue of Pillow not working on Wayland. I made one change in my Ubuntu 21.10 vm: modifying /etc/gdm3/custom.conf to uncomment #WaylandEnable=false and then ran sudo systemctl restart gdm3 to restart it so Ubuntu is using X instead of Wayland. Both ImageGrab.grab() and the mss module's screenshot function worked perfectly.

Thanks! It worked for me! 🦆 🥂

@mangatsukhbeer
Copy link

I was having the same issue, i read this thread and logged out of Ubuntu and changed my display protocol from wayland to xorg (using the small dial at the bottom right corner on the login screen) and ImageGrab.Grab() works perfectly now.

@tgwrite
Copy link

tgwrite commented Aug 16, 2022

This is definitely an issue of Pillow not working on Wayland. I made one change in my Ubuntu 21.10 vm: modifying /etc/gdm3/custom.conf to uncomment #WaylandEnable=false and then ran sudo systemctl restart gdm3 to restart it so Ubuntu is using X instead of Wayland. Both ImageGrab.grab() and the mss module's screenshot function worked perfectly.

Thanks! It worked for me!
yeah!

@JustStare
Copy link

这绝对是 Pillow 在 Wayland 上不起作用的问题。我在我的 Ubuntu 21.10 vm 中进行了一项更改:修改/etc/gdm3/custom.conf为取消注释#WaylandEnable=false,然后运行sudo systemctl restart gdm3以重新启动它,因此 Ubuntu 使用 X 而不是 Wayland。两者ImageGrab.grab()和 mss 模块的屏幕截图功能都运行良好。

thanks!!!!!!!!!!!!!!!

@nrg4
Copy link

nrg4 commented Jan 30, 2024

This is definitely an issue of Pillow not working on Wayland. I made one change in my Ubuntu 21.10 vm: modifying /etc/gdm3/custom.conf to uncomment #WaylandEnable=false and then ran sudo systemctl restart gdm3 to restart it so Ubuntu is using X instead of Wayland. Both ImageGrab.grab() and the mss module's screenshot function worked perfectly.

It is worked !!! Thanks a lot!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests