简体   繁体   中英

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. This is because the script needs to run on different systems without Python installed on it.

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. 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.

I think it has something to do with pyscreenshot, but maybe some of you have experience with pyscreenshot in .exe?

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 .

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 .

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