I need to write a python script which opens a website and when the website is completly opened it takes a screenshot of the opened website.
I wrote sth like this:
import webbrowser
import wx
wx.App()
link = "http://stackoverflow.com/questions"
webbrowser.get('firefox %s').open_new_tab(link)
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem
bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)
It only opens a new tab in firefox but it doesnt take a screenshot of it :(
I want the solution to be cross-platform. Thanks for any help:)
EDIT:
The main problem here is that the script musnt take a picture BEFORE my webpage is completly opened. How to solve that issue?
I believe that you need different solutions for different operating systems. Use sys.platform to find out on which platform you are on. The rest you have to figure out yourself, but a quick internet search revealed:
On Linux systems, you can then take a screenshot as described here:
Take a screenshot via a python script. [Linux]
On Windows systems, you can base your solution on this answer:
Fastest way to take a screenshot with python on windows
On Mac systems, this will help:
Take screenshot in Python on Mac OS X
This solution is fairly cross-platform, but if you're trying to show a bit of a web page open in a desktop with menu/toolbars etc... it's not what you want.
You could use SeleniumHQ to automate a browser of your choice, have that browser render the page, then get the complete image of the rendered page - ie, not just a screenshot of the portion of the page that's displayed on screen. You could then crop that accordingly.
According to this question, you should be able to use ImageGrab to take a screenshot. Since ImageGrab uses PIL, this should be cross-platform. Here is some sample code to get you on your way
Hope this helps
Related
I used pyautogui and PIL to capture screenshot after clicking on battery icon. But the screenshot doesn't contain battery status window. When I use print-screen key, status window is available in the screenshot. Need suggestions to solve this.
You can use greenshot opensource tool for windows, using hotkey you can take almost anything as a screenshot.
I think PrintScreen is best for taking screenshots on windows 10. But if you want to use any third-party apps for taking screenshots then pick-pick is a free and good app. It also allows you to take a fullscreen screenshot.
Is it possible to take screenshots of a running program (with GUI) from another python program ?
If so, what could be the steps and libraries that I could use ? (On Windows)
For example, let's say I have calc.exe running. I'd want to take screenshots of what is displayed to the user from myprogram.py.
My goal is to analyze what's displayed on the monitored program.
If it's not possible to isolate the screenshot to a running predefined program, I think I will have to take screenshots of the fullscreen but it's not very practical.
Capturing an screenshot is easy. Just install the Python Imaging Library and use the ImageGrab.grab() function to return an Image instance with the screenshot.
Capturing an specified window is a little more complicated, because you need the window coordinates. I recommend you to install the win32api modules and use a little module called winGuiAuto.py. Once you do that, you can do something like this:
hwnd = winGuiAuto.findTopWindow(title)
rect = win32gui.GetWindowPlacement(hwnd)[-1]
image = ImageGrab.grab(rect)
However, capturing the screen is the easy part. If you want to analyze the contents from screenshots, you're in for a lot of complications. This is probably the wrong approach for doing what you want and should be left as a last resort.
In most cases, it's easier to use the windows api to read the contents of a window's elements directly, but that won't work with some 3rd party GUI toolkits. That's not within the scope of your question so I'm not detailing it here, but you should read the source of the winGuiAuto.py module mentioned above for examples on how to do that, as well as checking the pywinauto library.
The ImageGrab Module, works on Windows only. The pyscreenshot module, is a better replacement for that, can be used to copy the contents of the screen to a PIL or Pillow image memory. Read more at link below.
https://pypi.python.org/pypi/pyscreenshot
I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:
"""
A simple screen grabbing utility
#author Fabio Varesano -
#date 2009-03-17
"""
from PIL import ImageGrab
import time
time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")
But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!
You can use Selenium and any browser:
Take a screenshot with Selenium WebDriver
This is cross-platform, works on every browser.
Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:
http://pypi.python.org/pypi/selenium
The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.
This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'
I am trying to send a couple basic text commands to a flash program running in Firefox on Windows 7, but I am unable to get pywinauto working for me.
Right now, I have just been able to accomplish the very basic task of connecting to Firefox plugin-container by directing it to the path using the following code:
from pywinauto import application
app = application.Application()
app.connect_(path = r"c:\Program Files (x86)\Mozilla Firefox\plugin-container.exe")
The next step seems to be something to the effect of:
app.plugin-container.Edit.TypeKeys('Text')
However, I can't reference the plugin-container window using '.plugin-container', or any combination of those words. I have tried adding a title variable to the connect_() function and I have tried everything I can think of to find out how to type the command.
The example I am basing this off of is the notepad sample:
from pywinauto import application
app.start_(ur"notepad.exe")
app.Notepad.Edit.TypeKeys(u"{END}{ENTER}SendText d\xf6\xe9s "
u"s\xfcpp\xf4rt \xe0cce\xf1ted characters!!!", with_spaces = True)
It doesn't matter to me if I use pywinauto or Firefox. If it is any easier to do this using a different module or Internet Explorer, I'm on board for whatever accomplishes the task. I am using Python version 2.7.2 and would prefer it over any version changes.
Any help at all is appreciated. I am pretty lost in all this.
As the author of pywinauto - I think you are going to have a hard time. pywinauto only really helps with standard windows controls, and I don't think that flash controls are implemented as standard windows controls (Buttons, Edit boxes, etc).
OFf the top of my head - I would think Sikuli may be a better starting point (http://sikuli.org/).
Another option may be 'http://code.google.com/p/flash-selenium/' - I just googled for "automating flash input" - and it turned up in one of the first articles I clicked.
Thanks for trying pywinauto - I just don't think it is best suited for Flash automation.
For example, I open a pdf file or a web page in gnome, use mouse double click some text, so a word is selected, how can I get this word in a background running daemon written with python-dbus?
Some simple but working piece of script is appreciated greatly.
Thanks!
You don't need D-Bus, simply listen to changes for the middle-click (Selection) clipboard with for example Gtk:
import gtk
def _clipboard_changed(clipboard, event):
text = clipboard.wait_for_text()
clip = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
clip.connect("owner-change", _clipboard_changed)
Gnome Do has a few plug-ins that use the selected text. I'm not sure how it is implemented (and if it uses DBus), but the code should reveal all. :)