简体   繁体   中英

pyscreenshot as ImageGrab doesn't work after compiling with PyInstaller to exe

When I use the code normally before compiling it works perfectly but when compiling to.exe with cxfreeze or pyinstaller and running the software freezes

import pyscreenshot as ImageGrab
import threading

def wprint():
    print("Wprint")
    global tempimg
    imagem = ImageGrab.grab()
    tempimg = imagem
    print("End Wprint")


x = threading.Thread(target=wprint)
x.start()

If we turn on logging, we can see that the module works by spawning a process to run PIL in

DEBUG:pyscreenshot.loader:running "pil" in child process
DEBUG:easyprocess:command: ['C:\\Users\\user\\.virtualenvs\\random\\Scripts\\python.exe', '-m', 'pyscreenshot.cli.grab', '--filename', 'C:\\Users\\user\\AppData\\Local\\Temp\\pyscreenshotbza0zwqm\\screenshot.png', '--backend', 'pil', '--debug']
DEBUG:easyprocess:process was started (pid=29996)
Wprint
DEBUG:easyprocess:process has ended, return code=0
DEBUG:easyprocess:stdout=
DEBUG:easyprocess:stderr=
DEBUG:PIL.PngImagePlugin:STREAM b'IHDR' 16 13
DEBUG:PIL.PngImagePlugin:STREAM b'IDAT' 41 65536
End Wprint

When you package your application, it'll try to run the new process against your generated exe as the interpreter (the first argument above), but your script doesn't handle anything from the arguments so it's just started again, which spawn an another process, and this will repeat until your resources are exhausted or until you terminate it.

The pypi home page of the pyscreenshot package mentions that it's mostly obsolete as PIL (pillow) now properly handles what it tried to initially solve, and it appears that it wasn't necessary on your platform. So you can also give pillow's ImageGrab a try directly which should have a normal Python API.

The pyscreenshot module is obsolete in most cases. It was created because PIL ImageGrab module worked on Windows only, but now Linux and macOS are also supported. There are some features in pyscreenshot which can be useful in special cases: flexible backends, Wayland support, sometimes better performance, optional subprocessing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM