Locate an element in an image resized in Python - python

I'm create a project in Python using PyAutogui and I need to locate an object on the screen. First of all I crop a screenshot, and after that, I use pyautogui.locateOnScreen('element.png') for found the location. The problem is, the element sometimes is resized so this method can't found it. I tried use confidence but not work properly with that. How can i locate that element using pyautogui or something different?
This is my code:
png = ["Andrew","David","Emma","Jay","Jesse","Josh","Mickey"]
screen = pyautogui.screenshot(region=(x+1,y+1,1281,751))
for element in png:
if(pyautogui.locateOnScreen(element + '.png',confidence=0.7) != None):
print("Find " + element)

Related

Why does the search image in my code doesn't work?

This is my code:
import pyautogui
images = ['colordonkergrijsDCphrasev2.png'] #the image I want it to find
while True:
for image in images: #search for the image in images
pos = pyautogui.locateOnScreen(image, region=(740,870, 50, 20)) #search on the screen for the image
if pos is not None: # this checks that the image was found
pyautogui.click(pos) # click the position of the image
I want that my code clicks on a special region when he is seeing that image.
But my code doesn't do that, my code clicks every time also when the image isn't there. The image I'm using is very similar as the background. But I added confidence = 1 and it still doesn't work
Does someone know how to fix it?
I use Python 3.9.4 64-bit.
I already read the docs but there isn't anything in there what can help me.
If you want to docs here it is: https://pyautogui.readthedocs.io/en/latest/screenshot.html

Take screenshot of webelement in Selenium Python and save file

I am trying to take a screenshot of a particular webpage element based on class name. I have followed the methods described in How to take screenshot with Selenium WebDriver, How to screenshot a specified WebElement in Selenium using Python and How to take partial screenshot with Selenium WebDriver in python?
Following are the commands and their errors:
driver.find_element_by_class_name("views-field-body").screenshot("test.png")
and driver.find_element_by_class_name("views-field-body").screenshot_as_png
Both times I get the error message as
selenium.common.exceptions.WebDriverException: Message: unknown command: session/75c3765173a9cf726d35afa7978d9b6e/element/0.5926184656216698-3/screenshot
When I try
image = driver.find_element_by_class_name("views-field-body").screenshot
this commands executes, but the image object comes out as bound method as shown in the quoted text below
bound method WebElement.screenshot of selenium.webdriver.remote.webelement.WebElement (session="75c3765173a9cf726d35afa7978d9b6e", element="0.5926184656216698-3")
How does one save this bound method to image on disk? Why are the commands not executing? Using Python 3.8, if that matters.
I think you use firefox not chrome
I found a solution
from selenium import webdriver
from PIL import Image
fox = webdriver.Firefox()
fox.get('https://stackoverflow.com/')
# now that we have the preliminary stuff out of the way time to get that image :D
element = fox.find_element_by_id('hlogo') # find part of the page you want image of
location = element.location
size = element.size
fox.save_screenshot('screenshot.png') # saves screenshot of entire page
fox.quit()
im = Image.open('screenshot.png') # uses PIL library to open image in memory
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = im.crop((left, top, right, bottom)) # defines crop points
im.save('screenshot.png') # saves new cropped image
https://stackoverflow.com/a/37565356/8951071

Selenium, Download Dynamic Captcha, Python

Because of the need to check like 100+ registration numbers, I decided to create a script which will do this for me, as I need to test these often.
The idea is that I have to visit the following website:
https://www.anaf.ro/inactivi/index.jsp
Where I input a registration number in the first field, and I have to input the captcha in the second. The captcha is quite easy to solve, and it is straight forward, as there are already available libraries for python, but the problem that I am facing is that the "src" attribute of the image, goes to a dynamic url.
Is there any way of saving the image which is displayed when using the .get on the webdriver?
This is how I am trying to solve the problem:
from captcha_solver import CaptchaSolver
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.anaf.ro/inactivi/index.jsp")
time.sleep(5)
elem = driver.find_element_by_name('inputCui') # Find the first input box
elem.send_keys('17741254') # Input the desired code
Or maybe, if someone has another idea on how I can approach the problem, I am open to suggestions.
Ok, so I managed to solve it, I will simply take a screenshot of the page, then crop it, after that I will decode the captcha. Here is the code for those interested:
def get_captcha(driver, element, path):
location = element.location
size = element.size
driver.save_screenshot(path)
image = Image.open(path)
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
image = image.crop((left, top, right, bottom))
image.save(path, 'png')
img = driver.find_element_by_xpath("//img[1]")
get_captcha(driver, img, "captcha.png")

Python 3.6.3 pyautogui.locateOnScreen not working

I have a "find.png" (attached)
The pyautogui.locateOnScreen is returning None, even after 20 screenshots!!!!
Using interpreter (code attached)
Note: 1) the image having the line, "import pyautogui as auto", is "find.png"
2)I have "find.png" in the same directory, it is founded( checked with PIL- Image)
After struggling with this forever also, finally figured out that you either use command line or print screen button with the windows key to take the screenshot. Using the snipping tool does not work
so try:
image = pyautogui.screenshot()
image.save('testing.png')
Go and crop testing.png as small as possible so that locateOnScreen works faster. Then go back to the terminal and type:
pyautogui.locateOnScreen('testing.png')
Try this:
location = pyautogui.locateOnScreen('testing.png', confidence =.8)
None simply means that PyAutoGui was unable to find your image on the screen, do remember the window is active where find.png was sampled from.
If this doesn't work out then I believe the resolutions(from when you took the sample and the monitor used by you) are different.
Let us know if you face any problem.

Printing main window in PyQt

I need to be able to save the main window of a pyqt app in a PS or similar file format so that I can send it to a printer. I would just make a built in screen shot function but my main window exceeds the size of my screen. Anyone know of a way to capture the window in it's entirety or is there a prebuilt class that could do this?
QPixmap has the static method grabWidget.
Pointing this method at your window will give you a pixmap that you can save to a file or use for printing.
If calling from inside your main window class:
sshot = QPixmap.grabWidget(self)
sshot.save('sshot.png')
QPixmap.grabWiget has been deprecated. We can instead use QWidget.grab() function instead to capture window. However, it only captures the currently visible parts of the screen which can be a problem when you have a window with a scroll area. So the only method/hack that worked for me was to use ScrollArea's page step functionality paired with widget grab.
# Get total pages in window
page_count = self.scrollArea.verticalScrollBar().maximum() / self.scrollArea.verticalScrollBar().pageStep()
image_list = []
# iterate through each page step
for i in range(int(round(page_count)) + 1):
step = self.scrollArea.verticalScrollBar().pageStep() * i
self.scrollArea.verticalScrollBar().setValue(step)
# capture and save each image
self.scrollArea.grab().save(f"page - {i}.jpg", quality=100)
# convert all images to Pillow Image() to later convert to pdf
image_list.append(Image.open(f"report_page - {i}.jpg"))
# save as pdf file
pdf_file_name = f'pdf_file.pdf'
image_list[0].save(pdf_file_name, "PDF", resolution=100.0, save_all=True, append_images=image_list[1:])
# delete images if not neccessary
for i in range(len(image_list)):
os.unlink(f"page - {i}.jpg")
P.s. Please let me know if there a more elegant solution to this problem

Categories