Taking screenshot from a python script by selecting the area - python

I saw that post (that is really helpful : Take a screenshot via a python script. [Linux]) about taking a screenshot from python. It works well but I'd like to have the same behavior as gnome-screenshot : having the possibility to choose between :
Capturing the whole desktop
Capturing an active window
Capturing an area
Is there a way to do this in python, or, eventually, to use the gnome-screenshot application to do it, and then getting the file ?
I tried to find the perfect command line for gnome-screenshot to be launched without asking where to save the screenshot after by giving the path at the call, but I can't find it.
Thanks for your help!

I have a wrapper project (pyscreenshot) for scrot, imagemagick, pyqt, wx and pygtk.
If you have one of them, you can use it.
Capturing an active window is missing.
Install:
easy_install pyscreenshot
Example:
import pyscreenshot as ImageGrab
# fullscreen
im=ImageGrab.grab()
im.show()
# part of the screen
im=ImageGrab.grab(bbox=(10,10,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')

If you are not limited to using using gnome-screenshot specifically, ImageMagick's import command can save directly to file without an interactive prompt.
See here for details: http://www.imagemagick.org/script/import.php
In addition to the command line interface, there is also a Python API.

Related

How to display an image as result of true in a function

So I'm playing around with some code and I'm trying to design a program that displays an image if a true statement results from the input. An example would be as follows:
name = input('name: ')
if name == 'Sammi':
# Here is where I would put the command to open an image. In place of print or whatever.
# I need some help with exactly what function to put here though.
The idea is that if the input string matches 'Sammi', then an image of me will be displayed preferably in a separate window, but I'm not exactly sure if that's possible or practical.
I've seen some guides that use PIL but the process of downloading and installing the required software is really tedious and I just have to wonder if it is actually required. I primarily use PyCharm for my code and occasionally ill go over to Notepad++ but it's mostly PyCharm. I'm not sure if that information is helpful but I thought I would provide it.
The image I want to use is located on my desktop and ideally would use the path C:\users\sammi\OneDrive\Desktop\B&W_2.jpg
My question as stated before is: exactly what function will allow me to do this? When you answer I would also really appreciate it if you explain the purpose of certain operators like fromand or for example, or whatever other operators or functions are used. I'm still fairly new to this stuff but I want to get really good at it.
Short reply, if you are running Python from the Standard Library, you can't, directly. The Standard Library is mostly a back-end library, so it works with logic, but doesn't "show" anything. This means that there's no way with the Standard Library to show images, you can however launch a process which opens the images.
E.g. Microsoft Paint
import subprocess
def open_image(path:str) -> None:
command = f'mspaint {path}'
subprocess.Popen(command)
open_image(input('Type your path: '))
If you don't want paint or another application, you should find a library that does it, be it Pillow or others.
To do this "right", you will need to learn about GUI programming with a library such as tkinter. Otherwise, the quick and dirty way is to use Image.show() from PIL. The documentation states that this is only intended for debugging purposes, so you should avoid it as a permanent solution in a serious project.
First of all, it is a good thing to know how to install packages (the things you can import in your scripts to use functions others made). It will be usefull when doing more complicated projects.
In Pycharm, you can add packages to your project:
File -> Settings -> Project: Project_Name -> Python Interpreter
Clicking on the + icon and searching pillow
This is not really used because there are better ways of installing packages
These are the two main ways of installing packages:
Using pip (https://pip.pypa.io/en/stable/installation/)
Using conda (https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html#regular-installation)
Both of these allow you to install packages.
I would recommend using conda because it is easier to separate projects from one another and allows the installation of packages that are not available using pip.
You will then be able to install PIL in the command line using
pip install Pillow
or (conda requires more setup, I encourage you to read https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)
conda install -c anaconda pillow
After installing the package you want here pillow, you can then use it in your code:
from PIL import Image
#from is used to specify the package to import
#import is used to specify which module you want to import in that library.
#The module contains functions you can use. (It's more complicated than that, but for basics this is what is does)
name = input('name: ')
if name == 'Sammi':
im = Image.open("sample-image.png") #Open the image
im.show() #Show the image on screen
If you prefer to avoid using any PyPi you can use tkinter.
# As to not to pollute the namespace, from module 'tkinter' only import these classes, and make it
# so you don't have to type tkinter.Whatever() every time you want to have a class instance
from tkinter import Tk, PhotoImage, Label
# Allow for case-insensitive inputs.
if input('Name: ').casefold() == 'sammi':
# Initialize graphical window instance
window = Tk()
window.title("StackOverflow Example")
# Load the image
image = PhotoImage(file=r"C:\users\sammi\OneDrive\Desktop\B&W_2.jpg")
Label(window, image=image).pack()
# Bring the window to the front once it appears
window.lift()
window.attributes('-topmost', True)
window.after_idle(window.attributes, '-topmost', False)
# Make window appear with all parameters set as described above
window.mainloop()

How to take screenshot of background app in python 2.7 for mac

I have been trying to make a program that notifies me when a number changes on an app. I have been using ImageGrab and then pytesseract which works but I can only figure out how to take the screenshot when I can visually see the number. It would be very nice if there was a way to take an image of the app if it was minimized (not visible on the screen) so I could work on other things as it ran. I also need the picture of a certain part of that app I need to do a bounding box within the app of where the picture is taken.
This is what I am currently using which takes a certain part of the whole screen:
img = ImageGrab.grab(bbox=(1400,875,1445,905))
I think there might be a way to do it with Quartz but I could not find out how to do a region of a background app.
It is possible to take screenshots of windows that are running in the background with the screencapture api in MacOS. You can see the documentation by typing man screencapture in the terminal.
For your use case it would look something like this:
screencapture -l <windowId> -R <x,y,w,h>
As you mentioned, you can use Quartz to find the desired windowId:
from Quartz import CGWindowListCopyWindowInfo, kCGNullWindowID, kCGWindowListOptionAll
windowName = 'Desktop' # change this to the window you are looking for
def findWindowId():
windowList = CGWindowListCopyWindowInfo(
kCGWindowListOptionAll, kCGNullWindowID)
for window in windowList:
if(windowName.lower() in window.get('kCGWindowName', '').lower()):
return window['kCGWindowNumber']
return None
You can find a fully working example here
Note: From my testings, if you minimise(cmd + m) or hide(cmd + h) a window, taking a screenshot of it will only capture the moment before it was hidden. You would need to keep the window opened for it to work--but it is ok to keep it behind other windows. Tested on MacOS v10.15.7.

MacOS: get screen of inactive application

I am trying to get a application screenshot of another process that is in the background, after I minimise the process. Anyway I could do this?
Similar to the question here.
But I couldn't get it to work on my Macbook, since instead of win32gui I am using Quartz, as os.commands for getting the screen process and screenshots.
There should be an app on your computer named Grab. It's in the Application/Utilities folder. Grab is a screenshot utility that gives you more control over what to include in a screenshot. You can take a screenshot of any window by selecting that option from Grab.

pyautogui.locateOnScreen() Returns... Nothing?

I'm trying to use PyAutoGui's image recognition functions. (OS X)
Needless to say, I'm running into some slight issues that I can't seem to solve myself no matter where I look or what I do. I'm attempting to have PyAutoGui click on the Chrome shortcut based off a .png screenshot saved to my desktop.
Here's my code in terminal:
>>>import pyautogui
>>>chrome = pyautogui.locateOnScreen('/Users/ianscalzo/Desktop/chrome.png")
>>>
I get no backfire on my filepath, but it causes my shell/terminal to return nothing but go to a new line. (As shown in the code example above - Just causes terminal to go to a blank ">>>")
I don't really understand why it doesn't do anything but go to a new line, so any insight would be greatly appreciated.
Thank you so much!
After struggling with this forever also, finally figured out that you either use command line to take the screenshot or using the screenshot button with windows key. It doesn't work with the snipping tool.
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')

How to show a fullscreen image from the command line interface without a window manager using Python

I have a python script that, when executed, wait's until it gets input from the user. I now want to know if it is posible to keep showing an image fullscreen until the user has given the input? I have searched for a solution but all i can find are tools that window managers to show the picture, but this is not installed. It'll probably only run on Debian.
I'm kind of searching for the same idea as omxplayer, but instead of movies it has to display pictures.
Using pygame is probably the easiest way of displaying an image fullscreen on the Linux framebuffer or on the X Windows root window (i.e. without a window manager).
The answers to the question Frame buffer module of python have all the details on how to achieve this.

Categories

Resources