1

I'm trying to take a screenshot using python that would work on both Windows and Linux. I've read pyscreenshot could do the job. But I have an error and the documentation doesn't seem to specify any dependency.

import pyscreenshot as ImageGrab
im = ImageGrab.grab()
im.show()

Traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/procutil.py", line 15, in _wrapper
    r = target(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 33, in _grab_simple
    return backend_obj.grab(bbox)
  File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/plugins/wxscreen.py", line 39, in grab
    im.frombytes(buffer(myWxImage.GetData()))
NameError: name 'buffer' is not defined

Traceback (most recent call last):
  File "ambi.py", line 10, in <module>
    im = ImageGrab.grab()
  File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 67, in grab
    to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
  File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 46, in _grab
    _grab_simple, imcodec.codec, to_file, backend, bbox, filename)
  File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/procutil.py", line 37, in run_in_childprocess
    raise e
NameError: name 'buffer' is not defined

I installed it with sudo pip3 install pyscreenshot

I tried installing wxscreen, but it doesn't seem to find a package with that name.

I don't want to use libraries that mimic keyboard inputs as the script will run in the background while playing games to monitor stats. Anti-Cheats could get triggered.

HypeWolf
  • 750
  • 12
  • 29
  • 1
    `buffer` is the name of a built-in type that existed in Python 2, but no longer exists in Python 3. I guess pyscreenshot's maintainers missed a spot while they were porting it :-) – Kevin Oct 24 '19 at 19:08
  • Ohhhhhhhhhhhhhhh! That makes total sense. Thanks for enlightening me. – HypeWolf Oct 24 '19 at 19:13
  • Interestingly, [the most recent version of pyscreenshot's wxscreen.py](https://github.com/ponty/pyscreenshot/blob/e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5/pyscreenshot/plugins/wxscreen.py#L7) _does_ check whether you're using Python 2 or not before using `buffer`. Maybe you're somehow using an older version of the package? – Kevin Oct 24 '19 at 19:25
  • Looks like this issue was [noticed and fixed back in June](https://github.com/ponty/pyscreenshot/issues/60), but since the PyPi distribution hasn't been updated since 2018, the fix isn't publicly accessible yet. If you're really dying to use this library, you could fetch it directly from github instead of using pip. – Kevin Oct 24 '19 at 19:35
  • @Kevin Or just pip-install from github, see [my answer](https://stackoverflow.com/a/70840405/5730279). Curious: How did you notice PyPi was not-updated since 2018 ? I saw `3.0 since Apr 18, 2021` in [release-history](https://pypi.org/project/pyscreenshot/#history) – hc_dev Jan 24 '22 at 21:14

4 Answers4

0

You can use pyautogui like:

import pyautogui

myScreenshot = pyautogui.screenshot()
Nader Alexan
  • 2,127
  • 22
  • 36
  • From my understanding this library mimic keyboards input? If I run this script while running a game, I think it could trigger an anti-cheat system though. (I'll clarify in the question) – HypeWolf Oct 24 '19 at 19:04
  • Ah ok, well, the `buffer()` function is only in Python 2, Python 3 there is `memoryview()`. Maybe try running your code with Python 2 – Nader Alexan Oct 24 '19 at 19:15
0
  • You can take a screenshot easily using hek library
import hek

hek.screen.screenshot(filename="test.png")
0

pip install git+

If the PyPi is not as up-to-date as you like and there is a git-repo you would like to use, then pip-install from the git-repo.

For example, as commented by Kevin because GitHub repo ponty/pyscreenshot contains the needed platform-switch between buffer and bytes since commit e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5, pip-install the package from there:

$ python3 -m pip install git+https://github.com/ponty/pyscreenshot.git@e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5

or use pip3 install git+ and so on.

See also:

hc_dev
  • 8,389
  • 1
  • 26
  • 38
-1

Another approach that is really fast is the MSS module. It is different from other solutions in the way that it uses only the ctypes standard module, so it does not require big dependencies. It is OS independant and its use is made easy:

from mss import mss

with mss() as sct:
    sct.shot()

And just find the screenshot.png file containing the screen shot of the first monitor. There are a lot of possibile customizations, you can play with ScreenShot objects and OpenCV/Numpy/PIL/etc..

Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • Well promoted, Tiger ️ No author affiliation or disclaimer?! Maybe we can use PyScreenshot's [MSS-wrapper](https://github.com/ponty/pyscreenshot/blob/master/pyscreenshot/plugins/msswrap.py) to have the best of both worlds ️ – hc_dev Jan 24 '22 at 21:04