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

[Windows] Black screen when used from Task scheduler (+fix) #149

Closed
graz68a opened this issue Feb 6, 2020 · 5 comments
Closed

[Windows] Black screen when used from Task scheduler (+fix) #149

graz68a opened this issue Feb 6, 2020 · 5 comments

Comments

@graz68a
Copy link

graz68a commented Feb 6, 2020

General information:

  • OS name: Windows10
  • OS architecture: 64 bits
  • Resolutions: 2k monitor Blenq
  • Python version: 3.7.32
  • MSS version: 5.0.0.

Description of the warning/error

I'm using this Python code to generate a Windows 10 desktop screenshot every 10 seconds
and save it in d:/screen/work/ folder. It works very well and extremely fast .
I execute the code from Windows command prompt (cmd.exe)

python d:/screeen/screen.py


import mss
import mss.tools
import datetime
import time

count = 0
while count < 100000000:
    with mss.mss() as sct:
        monitor = sct.monitors[1]
        timestr = time.strftime("%Y%m%d-%H%M%S")
        sct.compression_level = -1
        output = "d:/screen/work/" + (timestr) + ".png".format(**monitor)
        sct_img = sct.grab(monitor)
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)    

    time.sleep(10)
    count += 1

The problem starts when I try to automatize the script. I want the script starts automatically when I turn on the computer and I loggin in and I do not want the command window appears.
I followed this guide https://www.howtogeek.com/tips/how-to-run-a-scheduled-task-without-a-command-window-appearing/

When I login in computer the Pyhton script starts correctly , no command window appears,
but in my d:/screen/work/ folder are saved only full black screenshots !

How to avoid this ?

Thank you

@graz68a
Copy link
Author

graz68a commented Feb 6, 2020

I found now that someone reported the same here
#67 (comment)
"I have similar issue when running a script that takes a full screen screenshot; running the code on console works but running the script from a windows service app only returns a black screen"

Does exist any solution to this ?

@graz68a graz68a changed the title If I run the python script with a windows schedule nothing works running the Python script with a Windows schedule returns a black screen Feb 6, 2020
@BoboTiG
Copy link
Owner

BoboTiG commented Feb 6, 2020

Hello,

If you use the "Run only when user is logged on" option in the task, does it work?

@graz68a
Copy link
Author

graz68a commented Feb 6, 2020

No it does not work too.

Solution

However I found the solution, and the solution is executing it using pythonw.exe instead of python.exe .

@BoboTiG
Copy link
Owner

BoboTiG commented Feb 6, 2020

Ah, great news, thanks for the fix :)

And if I may, you could rewrite your code to something like that (using MSS.shot()):

import mss
import time

count = 0
with mss.mss() as sct:
    sct.compression_level = -1

    while "screenshotting":
        file_path = sct.shot(output="d:/screen/work/{date:%Y%m%d-%H%M%S}.png")
        count += 1

        print(file_path)
        time.sleep(10)

@BoboTiG BoboTiG changed the title running the Python script with a Windows schedule returns a black screen [Windows] Back screenshots when used from the Tasks Scheduler (+fix) Feb 6, 2020
@BoboTiG BoboTiG pinned this issue Feb 6, 2020
@BoboTiG BoboTiG changed the title [Windows] Back screenshots when used from the Tasks Scheduler (+fix) [Windows] Black screenshot when used from the Tasks Scheduler (+fix) Feb 6, 2020
@BoboTiG BoboTiG changed the title [Windows] Black screenshot when used from the Tasks Scheduler (+fix) [Windows] Black screen when used from Task scheduler (+fix) Feb 6, 2020
@BoboTiG BoboTiG closed this as completed Feb 6, 2020
@ubalklen
Copy link

I have the same problem. pythonw.exe didn't solve it.

My code is very simple:

# myscript.py
from mss import mss
from time import localtime, strftime
from random import random

PROB = 0.99

if random() <= PROB:
    with mss() as sct:
        filename = strftime("%d%m%Y_%H%M%S", localtime()) + ".png"
        sct.shot(mon=-1, output=filename)

I'm using the "Start a program action" in the task scheduler, with the following parameters:

  • Program/script: E:\Users\user\miniconda3\envs\myenv\pythonw.exe
  • Arguments: E:\Users\user\Documents\Code\myscript\myscript.py
  • Start in: E:\Users\user\Desktop

It doesn't work even when I'm logged on or if I trigger the task manually.

Interestingly, I can open a PowerShell from the desktop, run the exact command in the task (E:\Users\user\miniconda3\envs\myenv\pythonw.exe E:\Users\user\Documents\Code\myscript\myscript.py) and it works like a charm, so I'm guessing it's something specific to the scheduler.

@BoboTiG please let me know if you rather have a new issue for this.

@BoboTiG BoboTiG unpinned this issue Apr 9, 2023
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

3 participants