Screenshot with selenium and python - python

I need to take a screenshot of my entire screen for some automated tests I need to perform.
I was able to do this, using driver.get_screenshot_as_file , but the problem is that it only takes the picture of the web page, I need to get the whole picture from the browser, since the data I need to check is in the devtools.
Pic:
enter image description here
I need this:
enter image description here
Thankss!

You can use the package pyautogui to get screenshot of the desktop on the os level. This take screenshot of the entire desktop rather than just the webpage.
import pyautogui
pyautogui.screenshot().save('screenshot.png')

Another alternative to pyautogui would be PIL's ImageGrab. The advantage is that you are able to specify a bounding box:
from PIL import ImageGrab
image = ImageGrab.grab(bbox=None) # bbox=None gives you the whole screen
image.save("your_browser.png")
# for later cv2 use:
import numpy
import cv2
image_cv2 = cv2.cvtColor(numpy.array(image), cv2.COLOR_BGR2RGB)
This also makes it possible to adapt to your browser's window size and only capture its specific window. You can get your browsers bounding box as shown in this answer: https://stackoverflow.com/a/3260811/20161430.
From a speed perspective, it doesn't seem to make much of a difference whether you are using pyautogui or PIL.

Related

How to open an image in Paint and save it by Python?

I'm trying to write a code in python that is able to open an image in paint and save it with a different name of the original one.
My code is the follow:
import os
import subprocess
new_path='img.png'
path_img='./img.jpeg'
paintPath = os.path.splitdrive(os.path.expanduser("~"))[0]+r"\WINDOWS\system32\mspaint.exe"
subprocess.call([paintPath, path_img])
At this point, my image appears on Paint, but I would like automatically to save it and close the program, if it is possible. I didn't find any solution online.

How To Save Image Via Image Processing [Raspberry Pi]

I am creating a project with python and Raspberry Pi. I am trying to use my Webcam, as I, unfortunately burned my Camera Module. I was following along: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/image_processing/
Everything is working fine, except for one problem. I am not able to save the image file that is captured. I would like to take the photo I have created and turn it into a .jpg image. Code I have currently:
from imgproc import *
import cv2
# open the webcam
my_camera = Camera(320, 240)
# grab an image from the camera
my_image = my_camera.grabImage()
# open a view, setting the view to the size of the captured image
my_view = Viewer(my_image.width, my_image.height, "ETSBot Look")
# display the image on the screen
my_view.displayImage(my_image)
# wait for 5 seconds, so we can see the image
waitTime(0)
Can someone please help me with this problem?
Thanks in advance!
-Saurish Srivastava
Custom Library: https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/downloads/
UPDATE: It does not have to just use this type of code. You can give me an example with a different software. Just tell me how to use it properly so I don't mess up.
Adding the following in your code should save the image in the array my_image as picture.jpg
cv2.imwrite('picture.jpg', my_image)
For details on configuring raspberry pi-http://www.pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/

Image Recognition - pyautogui

I open up Calculator from window. I use the snipping tool to copy an image of the number 7 button. I paste the image into the paint software and save it as a png file and save it in a directory on my desktop.
I open up the calculator, use this code to locate where the image is on the screen. However the code return a blank space when normally it should return the position of the image on the screen. The first time I ran it, it gave me a coordination but the second time, it just shows me a blank space and I have been trying to figure out why. I kept doing it over and over, re-copied, re-saved the image and rerun the code and it's still the same result, blank. Was wondering what could be the reason.
>>> import pyautogui
>>> pyautogui.locateOnScreen('C:\\Users\\js\\Desktop\\jsPython\\seven2.png')
Maybe you should check your path string.For example, this code runs fine:
import pyautogui
print(pyautogui.locateOnScreen("C:\Python27\source\pyautogui\images\startIcon.png"))
I think you've made a typo in your path string.
Even better solution is to use absolute path.For example :
import pyautogui,os
print(pyautogui.locateOnScreen(os.path.abspath("images\startIcon.png")))

Python image recognition with pyautogui

When I try to recognize an image with pyautogui it just says: None
import pyautogui
s = pyautogui.locateOnScreen('Dark.png')
print s
When I ran this code the picture was on my screen but it still failed.
Pyautogui.locateOnScreen has a parameter that specifies the 'confidence' you have in the image you enter.
This way, pyautogui will deal with slight pixel deviations.
For example:
import pyautogui
s = pyautogui.locateOnScreen('Dark.png', confidence=0.9)
print(s)
For more information, see https://buildmedia.readthedocs.org/media/pdf/pyautogui/latest/pyautogui.pdf.
It's pixel perfect.
It can't find the image if it is not 100% match.
For example, I cropped an area with an Opera extension. Then I ran my script with Firefox, and pyautogui did not recognize it.
Don't let your image get resized or compressed by screen capture software or extensions.
Use the same window/screen (size, resolution) as where you saved your screenshot.
On my system, I get this if the picture is on a second monitor. If I move it to the main screen, the image is located successfully.
It looks like multiple-monitor functionality is not yet implemented:
From http://pyautogui.readthedocs.org/en/latest/roadmap.html
Future features planned (specific versions not planned yet):
Find a list of all windows and their captions.
Click coordinates relative to a window, instead of the entire screen.
Make it easier to work on systems with multiple monitors.
...

Ghost in Python isn't taking full resolution browser screenshots

Windows 7/Python 2.6
I am trying to take full browser screenshots and then use pillow to compare the images. I have started to use Ghost for the screenshots because i couldn't seem to get Selenium/PhantomJS to take full browser screenshots in headless mode. When i take a screenshot using Ghost the resolution of the images are like 780x8000 even thought i set the viewport size to 1920x680 (just testing resolution sizes while getting use to pillow). Sadly i can't share the screenshots but here is just is a snippet of code.
from ghost import Ghost
self.ghost = Ghost(viewport_size=(1920,680))
self.ghost.open('someurl')
self.ghost.capture_to('somedir')
After taking the screenshot the image is showing all the items in the webpage, but at the 1000px wide breakpoint for the layout.
Can someone either explain how to get the desired results of getting screenshots at 1920x"PageHeight" using either ghost or possible some other python package?
I have found the fix and it is to not set the viewport size in the constructor but using the set_viewport_size(x,y) method.
You should be able to take headless screenshots w/ selenium+phantomjs if you call
driver.set_window_size(x, y)
and then
driver.get_screenshot_as_file( "/path/to/img.png" )

Categories

Resources