简体   繁体   English

转换为.exe后,pyscreenshot不会截屏

[英]After converting to .exe, pyscreenshot doesn't take screenshots

I've been making a screen-to-text program in Python and I want to convert this to a .exe file. 我一直在用Python制作从屏幕到文本的程序,我想将其转换为.exe文件。 This is because the script needs to run on different systems without Python installed on it. 这是因为脚本需要在没有安装Python的不同系统上运行。

My code is as follows: 我的代码如下:

import numpy as np
import cv2
from PIL import Image
import pytesseract
import requests
import pyscreenshot

def main():
    currentWattage = 0
    while(True):
        imgGrab = pyscreenshot.grab(bbox=wattageBox)
        img = np.array(imgGrab)
        strRead = pytesseract.image_to_string(img, config="--psm 6")
        try:
            wattage = int(strRead)
            if(wattage != currentWattage):
                print("Wattage geupdate, lampen reageren")
                currentWattage = wattage
        except ValueError:
            print("Could not convert " + strRead)
        print(currentWattage)


if __name__ == "__main__":
    print("Start")
    pytesseract.tesseract_cmd = r'Tesseract-OCR\tesseract.exe'
    wattageBox = (150, 400, 240, 430)
    main()

If I run it through VS Code, it works fine. 如果我通过VS Code运行它,则效果很好。 I get the result I want. 我得到了想要的结果。 But when I convert it to an .exe file with cx_Freeze, it keeps restarting for some reason and fills up my memory. 但是,当我使用cx_Freeze将其转换为.exe文件时,由于某种原因,它会继续重新启动并填满我的内存。

I think it has something to do with pyscreenshot, but maybe some of you have experience with pyscreenshot in .exe? 我认为这与pyscreenshot有关,但也许有些人在.exe中有使用pyscreenshot的经验?

pyscreenshot uses multiprocessing, and one needs to call the multiprocessing.freeze_support() function to freeze a program which uses multiprocessing to produce a Windows executable, see the multiprocessing documentation . pyscreenshot使用多重处理,并且需要调用multiprocessing.freeze_support()函数来冻结使用多重处理生成Windows可执行文件的程序,请参阅多重处理文档

Try to add the following import to your code: 尝试将以下导入添加到您的代码中:

from multiprocessing import Process, freeze_support

and to modify its main part as follows: 并修改其主要部分,如下所示:

if __name__ == "__main__":
    freeze_support()
    print("Start")
    pytesseract.tesseract_cmd = r'Tesseract-OCR\tesseract.exe'
    wattageBox = (150, 400, 240, 430)
    Process(target=main).start()

This solution has been inspired by exe generated by py2exe and pyinstaller not working . 该解决方案受到py2exe生成exe的启发, 而pyinstaller无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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