I am trying to make an ambient light system with Python. I have gotten pyscreenshot to save a screenshot correctly, but I can't figure out how to get it to screenshot my second monitor (if this is even possible).
Is there a way to take a screenshot of my second monitor in Python using pyscreenshot (or something else)? I am using OSX Yosemite if that makes any difference.
Use the built-in screencapture command and pass it 2 filenames. I believe it lives in /usr/sbin/screencapture so the command will look like this:
/usr/sbin/screencapture screen1.png screen2.png
I assume you know how to shell out to it using the subprocess module, along these lines
from subprocess import call
call(["/usr/sbin/screencapture", "screen1.png", "screen2.png"])
Mark Setchell's answer is correct. Also this can't be done with pyscreenshot directly. If you look at the source, you'll notice that on Mac OSX they do use the screencapture utility but only pass it one file as an argument. The documentation (man screencapture) says that you have to pass in as many files as there are screens:
files – where to save the screen capture, 1 file per screen
Related
I would like to write a python script for use on Windows and Linux that clears the screen.
Most of the examples for this are something like:
import os
os.system('cls')
which works, but is a bit dicey given all of the issues with making system calls (security issues). Is there a better way of clearing the terminal in python without needing to use system?
The best alternative I have found so far was this:
print("\033c");
but it has the slight annoyance of removing everything from the terminal
(ie I would like it to clear the terminal, but the user should be able to scroll up and see previous output in the terminal).
The following ANSI escape code should help on linux (and most *nix unless you find a really weird terminal):
print("\x1b[2J\x1b[H",end="")
It'll clear the screen and put your cursor at the top left. You can still scroll up to find your old stuff but you may have to go up a decent distance to find it.
I have absolutely no idea what it'll do on windows. You may find you need to detect the os and use a different method there.
For python 2.x you'll need to use sys.stdout.write instead of the print statement as you can't suppress the \n on print in 2.x as far as I know.
If you have special knowledge of the screen size you can use a modified version of your original print-based answer.
def cls(x):
"""Clears the screen after printing x newlines."""
print "\n" * x
print "\033c"
In Python 3.3 and later you can divine the size of the Terminal window with shutil, but I don't think there's a great way to do it in 2.7 without actually importing os, which you said should be avoided.
This piece of code doesn't call os directly from the code.
Try this:
from subprocess import call
def clear(int=None):
call('clear')
if int == 0:
exit()
clear()
It worked for me, I work on linux but I think it will work on windows to.
What I'm trying to achieve is to open 2 CMDs (Windows) or Terminals (OS X/Mac). One for receiving input (via raw_input) and another for output (via print), I know this might be possible using Pythons process thingy, but I don't know how to go about this. If you don't understand I will repeat it:
-One CMD/Terminal where uses can type input
-Another CMD/Terminal where the user can see the output
I will need it in 1 file, not 2 different files.
Thanks in advance!
I'm afraid there is no simple way to do that. The natural way to have one window for input and one for output would be the tkinter, but that will not look like a CMD window in Windows.
As soon as you want to open a new terminal window, it will not be hidden behind one of the OS independant Python modules, and you will have to explicitely open two consoles and use their low level IO commands through ctypes under Windows, and open two terminals under OS X.
IMHO, unless you have a very strong need for it, you do not want such a complex thing.
I wanted to use Python to create animations (video) containing text and simple moving geometric objects (lines, rectangles, circles and so on).
In the book titled "Python 2.6 Graphics Cookbook" I found examples using Tkinter library. First, it looked like what I need. I was able to create simple animation but then I realized that in the end I want to have a file containing my animation (in gif or mp4 format). However, what I have, is an application with GUI running on my computer and showing me my animation.
Is there a simple way to save the animation that I see in my GUI in a file?
There is no simple way.
The question Programmatically generate video or animated GIF in Python? has answers related strictly to creating these files with python (ie: it doesn't mention tkinter).
The question How can I convert canvas content to an image? has answers related to saving the canvas as an image
You might be able to take the best answers from those two questions and combine them into a single program.
I've accomplished this before, but not in a particularly pretty way.
Tl;dr save your canvas as an image at each step of the iteration, use external tools to convert from image to gif
This won't require any external dependencies or new packages except having imagemagick already installed on your machine
Save the image
I assume that you're using a Tkinter canvas object. If you're posting actual images to the tk widgets, it will probably be much easier to save them; the tk canvas doesn't have a built-in save function except as postcript. Postscript might actually be fine for making the animation, but otherwise you can
Concurrently draw in PIL and save the PIL image https://www.daniweb.com/software-development/python/code/216929/saving-a-tkinter-canvas-drawing-python
Take a screenshot at every step, maybe using imagegrab http://effbot.org/imagingbook/imagegrab.htm
Converting the images to to an animation
Once the images are saved, I used imagemagick to dump them into either a gif, or into a mpg. You can run the command right from python using How to run imagemagick in the background from python or something similar. It also means that the process is implictely run on a separate thread, so it won't halt your program while it happens. You can query the file to find out when the process is done.
The command
convert ../location/*.ps -quality 100 ../location/animation.gif
should do the trick.
Quirks:
There are some small details, and the process isn't perfect. Imagemagick reads files in order, so you'll need to save the files so that alphabetical and chronological line up. Beware that the name
name9.ps
Is alphabetically greater than
name10.ps
From imagemagick's point of view.
If you don't have imagemagick, you can download it easily (its a super useful command-line tool to have) on linux and mac, and cygwin comes with it on windows. If you're worried about portability... well... PIL isn't standard either
There is a way of doing that, with the "recording screen method", this was explained in other question: "how can you record your screen in a gif?".
Click the link -->LICEcap : https://github.com/lepht/licecap
They say that it's free software for Mac (OS X) and Windows
You could look at Panda3D, but it could be a little over killed for what you need.
I would say you can use Blender3d too but i'm not really sure of how it works. Someone more experimented then me could tell you more about this.
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 collect data and parse it eventually from an open window in linux.
An example- Suppose a terminal window is open. I need to retrieve all the data that appears on that window. After retrieval, I would parse it to get specific commands entered.
So is it possible to do that? If so, how? I would prefer to use python to code this entire thing.
I am making a guess that first I would have to get some sort of ID for the open window and then use some kind of library to get the content from the window whose ID I have got.
Please help. I am quite a newbie.
You can (ab)use the assistive technologies support (for screen readers and such) that exist in the toolkit libraries. Whether it will work is toolkit specific—Gtk and Qt have this support, but others (like Tk, Fltk, etc.) may or may not.
The Linux Desktop Testing Project is a python toolkit for abusing these interfaces for testing GUI applications, so you can either use it or look how it works and do similar thing.
I think the correct answer may be "with some difficulty". Essentially, the contents of a window is a bitmap. This bitmap is drawn on by a whole slew of primitives (including "display this octet-string, using that encoding and a specific font"), but the window contents is still "just pixels".
Getting the "just pixels" is pretty straight-forward, as these things go. You open a session to the X server and say "given me the contents of window W" and it hands it over.
Doing something useful with it is, unfortunately, a completely different matter, as you'd potentially have to (essentially) OCR the bitmap for what you want.
If you decide to take that route, have a look at the source of xwd, as that does, essentially, that.
Do you have some sort of control over the execution of the terminal? In that case, you can use the script command in the terminal session to log all interaction to a file and then read and parse the file.
$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile
If the terminal is running inside of screen, you have other options as well. Screen has logging built in, screen -X sends commands to a running screen session (man screen).