PyAutoGUI - Locate anything on your screen | Simple Pyautogui project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello coders in this video i am going to show you the most important function of pi auto gui package this function is so important that you will use it in almost every automation project that you create and i will also be creating a simple project using this function so let's get started i am inside my vs code i'll create a new file set the language to python and save the file on my desktop now i'll come here those who have not seen my previous video let me tell you i am using a python package named pi auto gui which is used to automate computer guise so you will have to install it like this type pip install pi auto gui i have already installed this package so now coming to the code i'll import the package now if you want to get the location or coordinates of any part on the screen then there is a function named locate on screen this function takes an image as a parameter and returns the full location of that image on the screen for example if i want to get the location of this edit menu then what i will do is i'll search for snipping tool on my computer like this open it click on new and then capture this edit menu area and save it as a png file or any image type file now i will pass the name of this image file over here let me print the response i'll come here and run the code you can see it has printed the full location it says from left it's at 116 and from top it's at 6. these are the coordinates x and y where x is 1 1 6 and y is 6 then it gives the width and height of that image but we just require the coordinates that is the x and y value so in order to do that there is another function named center let me show you i'll pass the response to the center function and it will return to me the center coordinates of that part let me run this and there you go it has given me the exact center coordinates now i'll move to that location using the move to function but before that i'll have to take the coordinates inside a variable like this and then pass it to the move to function by the way move to function i have explained it in detail and there are more functions that i have explained in my previous video so do check that out what move to function does is whatever the coordinates that you pass to this function it is going to move your mouse to that particular location or to that particular coordinate that's it okay let's run this did you see that my mouse automatically came to this edit menu so you had to perform two operations in order to get the location of a part on the screen but you can also do this using one operation that is by using a function named locate center on screen this function is the combination of locate on screen and center functions here you need to pass the image file name and it will return the center location of that image i'll just print the response and now run the code you got the same result let me just write the comments for you just to understand so you can use only this function and your work is done so coders this was the most important function of buy auto gui package which you will require in almost every automation project now is the time to create a simple project using this function and other functions that i showed you in my previous video the project that i'll create is subscribing a youtube channel automatically i have written down the steps already so the first step is to open a new tab on the browser see i know that shortcut for new tab is control plus t so to press ctrl and t i will use the hotkey function and i'll pass ctrl and t that's it the next step is to search youtube.com to do that i'll use the write function and pass the youtube url so this will write the url in the search bar and then i'll press enter using the hotkey function now we'll just run this much and see but there is one problem you'll see it when i run the code let's see i'll keep the browser open behind the editor like this i'm running the code and here is the problem you can see that it typed control plus ta and the url inside the terminal itself so we need to find a way to minimize our editor and run the code in the background in order to do this there are some functions in pi to gui package which can take inputs from you we will use one of those functions so the function i will use is prompt this function shows a pop-up and a text box to take inputs the attribute i'll pass is text which will store whatever i'll type in the text box and title to give a heading for the popup i'll type enter the channel name and this will return the text that i will enter so i'll store it in this variable i'll call it channel underscore name you can give whatever name you want i'll write the comment so let me show you the channel that i want to subscribe by the way i am not doing any paid promotions i've been following this channel so the channel name is the english scholar online camp if you want to learn grammar and punctuation improve your vocabulary get some useful tips and free resources then you can go ahead and check this channel and subscribe it one thing i love is that most of the videos on this channel are short ones okay so now i'll go back to the code here i'll print the channel name now i'll run the code again and here the popup is it says enter the channel name i'll type the english scholar online camp ok did you see that my code was running in the background and we were able to do what we wanted now the next step is to type the enter channel name in the search box so to locate the search box i'll capture this search using the snipping tool and i'll save it i'll also capture the subscribe button oh but for that first i need to unsubscribe okay now you can see the subscribe button now i will capture it using the snipping tool again and i'll save it going back to the code so use our important function again to locate the search box and also locate the subscribe button on the screen locate center on screen the file name once i locate it i'll move to that location so i'll use move to function then i need to click on the search box so i'll use the click method after clicking i need to type the channel name so i'll use the write function and then pass the channel name variable to that function i'll just write the comment okay then again to press enter pi auto gui dot hotkey enter i forgot one thing i also need to click after i press ok on the pop-up so i will have to add a click function there also here okay now i want to show you one attribute for the locate center on screen function that is confidence i'll type confidence equal to 0.9 0.9 means 90 percent so 1 will be 100 percent this means that even if there is a 90 match then still it should consider and to use this confidence attribute i need to install a package called opencv python so i'll type pip install opencv python done now let's run this code did you see that i didn't touch my mouse or keyboard it automatically opened youtube and searched for the channel so now the next step would be to open the channel to do that i need to click on the channel logo so now you know what i'm going to do open snipping tool capture the logo and save it now come back to the code i'll just copy paste this over here and change the file name that's it i'll put the necessary sleep of one second for the next and final step again i'll copy paste change the file name this image is the subscribe button that we captured earlier and that's it now let's run it oops it did not search the logo this might happen sometime due to pixel mismatch so here the confidence was 90 percent still it did not work so now i'd reduce it to 80 percent and try again in such cases you'll have to decrease or increase the confidence and keep trying that's why the confidence attribute is very important now i think it should work let's see bingo it worked i did not touch my mouse or keyboard it automatically opened youtube search for the channel name then opened the channel and subscribed it so by this i have completed almost all the functions of the pi auto gui package and showed you how you can automate anything on your computer using this package now i want you to follow this video step by step and then using your code subscribe to my channel and let me know in the comment section if you learned something from this video then please do not forget to subscribe to my channel in order to be notified about my latest videos thank you so much for watching see you in my next video
Info
Channel: Coding 101 with Steve
Views: 102,048
Rating: undefined out of 5
Keywords: pyautogui, pyautogui locate on screen, pyautogui projects, pyautogui 2022, simple pyautogui project, locate on screen pyautogui, pyautogui locate on screen and click, pyautogui tutorial, python pyautogui, pyautogui automate the boring stuff, pyautogui python tutorial, python3 pyautogui, python automation projects, pyautogui python, gui automation using python, python automation, computer GUI automation, locate on screen not working, pyautogui bot
Id: qIJpBz6R2Uw
Channel Id: undefined
Length: 12min 41sec (761 seconds)
Published: Thu Mar 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.