Python Forum
pyautogui.screenshot region is not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyautogui.screenshot region is not working
#1
When I try to capture a video screenshot of my screen in a region suddenly the .avi output file becomes a 7kb. The is no error, so I do not know how to fix this The output video is fine before selecting the region. how can i fix this problem?
# create the video write object
img = pyautogui.screenshot(region=(350, 800, 500, 150))  # --->saves 7kb .avi file
img = pyautogui.screenshot() #  --->works fine
Reply
#2
this code just returns the screenshot, it does not save anything. show your full code (minimal reproducible example)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Here is the full code
import cv2
import numpy as np
import pyautogui
# display screen resolution, get it from your OS settings
import tkinter as tk

root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
SCREEN_SIZE = (screen_width, screen_height)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
while True:
    # make a screenshot

    img = pyautogui.screenshot(region=(350, 800, 500, 150))
    # convert these pixels to a proper numpy array to work with OpenCV
    frame = np.array(img)
    # convert colors from BGR to RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # write the frame
    out.write(frame)
    # show the frame
    cv2.imshow("screenshot", frame)
    # if the user clicks q, it exits
    if cv2.waitKey(1) == ord("q"):
        break

# make sure everything is closed when exited
cv2.destroyAllWindows()
out.release()
Reply
#4
To start debug I would try and save img separately, so that you know what you actually get on line 17.

Also note that pyautogui just uses pyscreeze for screenshot functions, which in turn uses Pillow.ImageGrab on Windows. By the way Pillow.ImageGrab now works also on Mac, but pyscreeze still uses subrpocess call to screencapture
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
The problem was this part and changing the last two parameters solved the problem.
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
Reply
#6
thanks for posting back. how did you change them in order to work?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I just changed this part :
out = cv2.VideoWriter("output.avi", fourcc, 8.0, (500, 150))

but the problem is somehow remained. Still it is inconsistent. Like three times out of five times it saves the .avi file as 6kb again, specially if I change the saving path it is going to record as 6kb constantly. It is really strange.

changing
fourcc = cv2.VideoWriter_fourcc(*"XVID") to :
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
also solved the problem with inconsistency.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyautogui, İmagesearch, Moving target Beyazx 0 550 Jun-27-2023, 08:47 PM
Last Post: Beyazx
  PyAutogui write Dollar Sign Dutch keyboard not working alato 0 780 Nov-22-2022, 11:25 PM
Last Post: alato
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,222 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  PyautoGUI- How to use If - Else with pyautogui.locateCenterOnScreen Tiel 3 8,127 Jun-27-2022, 02:00 PM
Last Post: DeaD_EyE
  pyautogui.locateOnScreen producing error dude8074 6 3,731 Apr-17-2022, 05:05 PM
Last Post: bowlofred
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,482 Feb-03-2021, 01:16 PM
Last Post: rachidel07
Sad problem with Pyautogui rachidel07 1 2,421 Jan-27-2021, 05:43 PM
Last Post: nilamo
  Add new line after finding last string in a region Nigel11 1 1,844 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  pyautogui with a display emulator? gumby4231 0 2,550 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  pytz: get integer utc offset from Olsen region/city h4tt3n 2 5,085 Jul-30-2020, 06:43 AM
Last Post: h4tt3n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020