I'm using pyautogui to take few screenshots per second and save them.
pyautogui save the screenshot without the cursor, can I take the screenshot with it by default (without edit the photo)?
This is the code I'm using:
import time
import pyautogui
def takeScreenshot(count):
myScreenshot = pyautogui.screenshot()
myScreenshot.save(r'imgs\screenshot_'+str(count)+'.png')
time.sleep(0.2)
takeScreenshot(count+1)
takeScreenshot(0)
Related
I am trying to make a shortcut to open google when I do the function normal it works but when I try to do it in a shortcut it doesn't work.
import pyautogui as pg
import keyboard
hotkey1 = "ctrl+alt+w"
def google():
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
while True:
if keyboard.is_pressed(hotkey1):
google()
You only need a pause until pg can take over. It so happens that when you press the hotkey combo, you might accidentally hit some other keys and therefore it doesn't work as intended. This should give it enough time for the key to be depressed before the automatic process starts.
import time
def google():
time.sleep(0.25)
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
Below is my code :
import time
import pyautogui
location = pyautogui.locateOnScreen('ok.png')
pyautogui.click(location)
Now I don't know when the "OK" image appears. So I want to loop the code to search for the image until it is found. Otherwise it is terminating immediately before the image appears.
Once found break the loop and click that image.
How do I do I do that?
And time.sleep() dosent work because the image can appear anytime.
I have done something similar,
Using the autoit module to find the window title and button name
import autoit,time
while True:
try:
autoit.control_click("[TITLE:MTMS Update; CLASS:#32770]", "Button1")
print("Clicked OK")
except:
time.sleep(30)
pass
I’m doing some testing activity which requires me to capture screenshot of applications/DB etc and save it to a document. The whole activity has more than 50 screenshots. Is there a way in Python through which I can take a screenshot with windows shortcut key( eg; CTRL ALT shift C) and it appends the image to a document file. I believe the python program should be running in background like nohup in Unix.
For storing screen captures in Word using a hotkey, you can use a combination of libraries.
Use win32gui to open Word
Use python-docx to update the document and save
Use PyAutoGUI to do the screen capture
Use keyboard to listen for the hotkey
For this script to work, you will need to create the Word document before running the script.
# Need these libraries
# pip install keyboard
# pip install PyAutoGUI
# pip install python-docx
# pip install win32gui
import keyboard
import pyautogui
from docx import Document
from docx.shared import Inches
import win32gui
from PIL import ImageGrab
shotfile = "C:/tmp/shot.png" # temporary image storage
docxfile = "C:/tmp/shots.docx" # main document
hotkey = 'ctrl+shift+q' # use this combination anytime while script is running
def do_cap():
try:
print ('Storing capture...')
hwnd = win32gui.GetForegroundWindow() # active window
bbox = win32gui.GetWindowRect(hwnd) # bounding rectangle
# capture screen
shot = pyautogui.screenshot(region=bbox) # take screenshot, active app
# shot = pyautogui.screenshot() # take screenshot full screen
shot.save(shotfile) # save screenshot
# append to document. Doc must exist.
doc = Document(docxfile) # open document
doc.add_picture(shotfile, width=Inches(7)) # add image, 7 inches wide
doc.save(docxfile) # update document
print ('Done capture.')
except Exception as e: # allow program to keep running
print("Capture Error:", e)
keyboard.add_hotkey(hotkey, do_cap) # set hot keys
print("Started. Waiting for", hotkey)
keyboard.wait() # Block forever
I'm trying to create a script to open a webpage and to type stuff there, but first I wanted to try to do it using Notepad
import time
import os
import webbrowser
import pyautogui
os.system("notepad.exe")
#time.sleep(3)
pyautogui.write('Hello world!', interval=0.25)
This is what I have so far. The problem is that it only opens the Notepad and only after I close the Notepad, 'Hello world!' is printed on my terminal where I'm running the script.
What am I missing here? Is there any other way to write text with a python script?
Thanks!
This is called file handling
with open('Filename.txt', 'w') as f:
f.write('What you want to put in')
But this also works
import time
import os
import webbrowser
from pynput.keyboard import Key, Controller as K
from pynput.mouse import Button, Controller as M
os.startfile("notepad.exe")
M().position = (900,400)
M().click(Button.left, 1)
#time.sleep(3)
K().type('Hello world!')
You might want to use pyautogui's mouse functions to click the notepad and then type it. For more information on the mouse functions, go here:
https://automatetheboringstuff.com/2e/chapter20/
I have currently written an python script that takes input from the user's video camera with the following code:
import numpy as np
import cv2
import cv
import datetime
import math
import time
import os
cap = cv2.VideoCapture(0)
def init():
ret, imgBase = cap.read()
print(type(imgBase))`
The above code is completely functional and it does get input from the user. However, instead of getting input from the user's web cam, I want to get input from user's screen. I need something like cap = cv2.ScreenCapture(0) I understand that that doesn't work, but I need something that does a similar function so I can read and capture an image from the user screen. In essence, I need to get a screenshot of the monitor/user's screen.
Only works on Mac:
import os
os.system('screencapture out.jpg')
You can read the screenshot into memory using default python functions.
For further info type man screencapture in a terminal or check this link: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/screencapture.1.html.