pywinauto capture_as_image adds unwanted borders - python

I am using pywinauto to take a screenshot of a specific window.
Here is the code I use to take a capture of notepad ("Bloc-notes" in french) :
from pywinauto import Application
app = Application().connect(title_re=".*Bloc-notes")
hwin = app.top_window()
hwin.set_focus()
img = hwin.capture_as_image()
img.save('notepad_screenshot.png')
And here is the result:
The red "border" is the background of the window. How can I safely eliminate this red border?
I tried to configure Windows 10 in order not to show windows shadows (in the "visual effects settings") but it has no effect on the size of the capture.
When I look precisely on the capture, I can see that the left, bottom and right borders are 7 pixels thick. Can I reliably remove these pixels? What I mean by "reliably" is: will it always work, and work on other computers?
Any help appreciated.

Here is the solution I found.
import ctypes
from pywinauto import Application
import win32gui
app = Application().connect(title_re=".*Bloc-notes")
hwin = app.top_window()
hwin.set_focus()
window_title = hwin.window_text()
rect = ctypes.wintypes.RECT()
DWMWA_EXTENDED_FRAME_BOUNDS = 9
ctypes.windll.dwmapi.DwmGetWindowAttribute(
ctypes.wintypes.HWND(win32gui.FindWindow(None, window_title)),
ctypes.wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS),
ctypes.byref(rect),
ctypes.sizeof(rect)
)
img = hwin.capture_as_image(rect)
img.save('notepad_screenshot_ok.png')
And here is the result:
It has worked on all the tests I run (different windows).

Application().connect can return a collection of windows.
Instead use app['YOUR TITLE HERE'] or use find_windows.
From there you can capture the image without those borders.
You can find more info from the docs.

Related

Python/Linux - Check if some other app is fullscreen

I have developed a controller for RGB LEDs on the back of my monitor, and I would like to control them so that they match the average color on the screen when I have a full screen app running, such as a movie.
I already have the whole controller up and running in the background, but I got stuck trying to figure out how to determine if there is some app running full-screen or not. How could i do it? I am using python3 on Debian testing.
Thanks a lot for any help!
I found an answer here and modified it a bit to make it more usable. Here is my code that works on gnome. You might have to adjust the escaped windows names for other gdms.
import Xlib.display
#Find out if fullscreen app is running
screen = Xlib.display.Display().screen()
root_win = screen.root
def is_fullscreen():
#cycle through all windows
for window in root_win.query_tree()._data['children']:
width = window.get_geometry()._data["width"]
height = window.get_geometry()._data["height"]
#if window is full screen, check it the window name
if width == screen.width_in_pixels and height == screen.height_in_pixels:
if window.get_wm_name() in ['Media viewer', 'mutter guard window']:
continue
#return true if window name is not one of the gnome windows
return True
#if we reach this, no fs window is open
return False

pygame fullscreen on second monitor [duplicate]

I am using pygame to program a simple behavioral test. I'm running it on my macbook pro and have almost all the functionality working. However, during testing I'll have a second, external monitor that the subject sees and the laptop monitor. I'd like to have the game so up fullscreen on the external monitor and not on the laptop's monitor so that I can monitor performance. Currently, the start of the file looks something like:
#! /usr/bin/env python2.6
import pygame
import sys
stdscr = curses.initscr()
pygame.init()
screen = pygame.display.set_mode((1900, 1100), pygame.RESIZABLE)
I was thinking of starting the game in a resizable screen, but that OS X has problems resizing the window.
Pygame doesn't support two displays in a single pygame process(yet). See the question here and developer answer immediately after, where he says
Once SDL 1.3 is finished then pygame will get support for using multiple windows in the same process.
So, your options are:
Use multiple processes. Two pygame instances, each maximized on its own screen, communicating back and forth (you could use any of: the very cool python multiprocessing module, local TCP, pipes, writing/reading files, etc)
Set the same resolution on both of your displays, and create a large (wide) window that spans them with your information on one half and the user display on the other. Then manually place the window so that the user side is on their screen and yours is on the laptop screen. It's hacky, but might a better use of your time than engineering a better solution ("If it's studpid and it works, it ain't stupid" ;).
Use pyglet, which is similar to pygame and supports full screen windows: pyglet.window.Window(fullscreen=True, screens[1])
Good luck.
I do not know if you can do this in OS X, but this is worth mentioning for the Windows users out there, if you just want to have your program to run full screen on the second screen and you are on windows, just set the other screen as the main one.
The setting can be found under Rearrange Your Displays in settings.
So far for me anything that I can run on my main display can run this way, no need to change your code.
I did something silly but it works.
i get the number of monitors with get_monitors()
than i use SDL to change the pygame window's display position by adding to it the width of the smallest screen, to be sure that the window will be positionned in the second monitor.
from screeninfo import get_monitors
numberOfmonitors = 0
smallScreenWidth = 9999
for monitor in get_monitors():
#getting the smallest screen width
smallScreenWidth = min(smallScreenWidth, monitor.width)
numberOfmonitors += 1
if numberOfmonitors > 1:
x = smallScreenWidth
y = 0
#this will position the pygame window in the second monitor
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
#you can check with a small window
#screen = pygame.display.set_mode((100,100))
#or go full screen in second monitor
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#if you want to do other tasks on the laptop (first monitor) while the pygame window is being displayed on the second monitor, you shoudn't use fullscreen but instead get the second monitor's width and heigh using monitor.width and monitor.height, and set the display mode like
screen = pygame.display.set_mode((width,height))
display = pyglet.canvas.get_display()
display = display.get_screens()
win = pyglet.window.Window(screen=display[1])
------------------------------------------------------
screen=display[Номер монитора]
------------------------------------------------------
display = pyglet.canvas.get_display()
display = display.get_screens()
print(display) # Все мониторы которые есть

Minimize OpenCV HighGUI Window

I see it is possible to resize window with OpenCV, for example:
import cv2
img = cv2.imread('Test.jpg')
cv2.imshow('image',img)
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.resizeWindow('image', 600,600)
cv2.waitKey(0)
cv2.destroyAllWindows()
But is it possible to minimize current window?
I think this is maybe cv2.setWindowProperty()this function for example here is fullscreen
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("window", img)
Well, there is no function/method in openCV official documentation to minimize the window automatically. You can try different method with python to do the task. Such method can be found here: Is there a way to minimize a window in Windows 7 via Python 3?
Though i'm posting it here too for complete reference:
To minimize a window you need to know either the title of the window, or its window class. The window class is useful when the exact window title is not known. For example the following script shows two different ways to minimize the Microsoft Windows Notepad application assuming:
import ctypes
notepad_handle = ctypes.windll.user32.FindWindowW(None, "Untitled - Notepad")
ctypes.windll.user32.ShowWindow(notepad_handle, 6)
notepad_handle = ctypes.windll.user32.FindWindowW(u"Notepad", None)
ctypes.windll.user32.ShowWindow(notepad_handle, 6)
To determine the class name to use, you would need to use an tool such as Microsoft's Spy++. Obviously if Notepad was opened with a file, it would have a different title such as test.txt - Notepad. If this was the case, the first example would now fail to find the window, but the second example would still work.
If two copies of notepad were running, then only one would be closed. If all copies needed to be closed, you would need to enumerate all windows which requires more code.
The ShowWindow command can also be used to restore the Window.
Maybe this code will help you, I use it to detect mouse events to minimize the opencv GUI when I click it, obviously, you can create another more interesting application with this:
import cv2
import numpy as np
import win32gui,win32con
a = np.zeros((200,200,3),np.uint8)
def tactil_sec(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.imshow("LOL",a)
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
cv2.namedWindow('LOL')
cv2.setMouseCallback('LOL',tactil_sec)
while 1:
cv2.imshow("LOL",a)
if (cv2.waitKey(20) & 0xFF == 27):
break
cv2.destroyAllWindows()

Python 3.4 Tkinter Claims Image Doesn't Exist [duplicate]

I am trying to set an application icon (python3 / tkinter) like this:
Interface()
root.title("Quicklist Editor")
root.iconbitmap('#/home/jacob/.icons/qle_icon.ico')
root.resizable(0, 0)
root.mainloop()
no matter what I do, I keep getting an error message (Idle), saying:
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: error reading bitmap file "/home/jacob/.icons/qle_icon.ico"
What am I doing wrong?
The problem is not the code, but the icon. I tried creating an xbm with another program than Gimp (some KDE icon editor), and although it looks terrifyingly ugly, it does show an icon.
I guess I have to find a creator that gives an "understandable" icon for my Python program.
Edit
The iconbitmap method turned out to be black and white only, so it was useless after all.
After a long search, I found the solution to set the color of an application's icon for Python 3 (on Linux). I found it here:
root = Tk()
img = PhotoImage(file='your-icon')
root.tk.call('wm', 'iconphoto', root._w, img)
This is an old question, and there is lots of stuff written about it on the web, but all of it is either incorrect or incomplete, so having gotten it to work I thought it would be good to record my actual working code here.
First, you'll need to create an icon and save it in two formats: Windows "ico" and Unix "xbm". 64 x 64 is a good size. XBM is a 1-bit format--pixels just on or off, so no colors, no grays. Linux implementations of tkinter only accept XBM even though every Linux desktop supports real icons, so you're just out of luck there. Also, the XBM spec is ambiguous about whether "on" bits represent black or white, so you may have to invert the XBM for some desktops. Gimp is good for creating these.
Then to put the icon in your titlebar, use this code (Python 3):
import os
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("My Application")
if "nt" == os.name:
root.wm_iconbitmap(bitmap = "myicon.ico")
else:
root.wm_iconbitmap(bitmap = "#myicon.xbm")
root.mainloop()
This will allow you to use PNG files as icons, and it does render color. I tested it on Xubuntu 14.04, 32-bit with Python 3.4 (root is your Tk object):
import sys, os
program_directory=sys.path[0]
root.iconphoto(True, PhotoImage(file=os.path.join(program_directory, "test.png")))
(Finding program directory is important if you want it to search for test.png in the same location in all contexts. os.path.join is a cross-platform way to add test.png onto the program directory.)
If you change True to False then it won't use the same icon for windows that aren't the main one.
Please let me know if this works on Windows and Mac.
I tried this, and I couldn't get it to work using Windows 7.
Found a fix.
Use Jacob's answer, but the file has to be a .gif if you're using my OS, (Windows 7) it appears.
Make a 64x64 gif using MS paint, save it, use the file path and bingo, works.
I hope this helps you for cross-platform ability
LOGO_PATH="pic/logo.ico"
LOGO_LINUX_PATH="#pic/logo_1.xbm" #do not forget "#" symbol and .xbm format for Ubuntu
root = Tk()
if detect_screen_size().detect_os()=="Linux":
root.iconbitmap(LOGO_LINUX_PATH)
else:
root.iconbitmap(LOGO_PATH)
Simply using an r string to convert the directory into raw text worked for me:
ex:
app.iconbitmap(r'enter your path here')
In my case, Ubuntu 20.04, python 3.6 (conda), the command iconbitmap(bitmap=icon_path) failed w/ this error. In the end, I put the command w/in a try-except block and it worked; I can see the colorful image.
I'm surprised to see this is such an old question with no good answers, not in eight years! I too want my own icon for my "quickie" tkinter program.
What does work for me on Linux and Python3:
#!/usr/bin/env python
import tkinter
from PIL import Image, ImageTk
root = tkinter.Tk()
im = Image.open('junk.png')
photo = ImageTk.PhotoImage(im)
root.wm_iconphoto(True, photo)
root.mainloop()
The key seems to be using Image and ImageTk. I found zero solutions that worked without these.

Python psychopy imageStim

I'm using psychopy to get an image on a window like this:
from psychopy import visual
window = visual.Window(size= (1000,600), units = 'pix', pos = (10,10))
stimulusimage = ('C:\Users\name\Documents\Python Scripts\Project\stimuli\Visual Stimuli\Positive\1.jpg')
showingimage = visual.ImageStim(window, image=stimulusimage)
window.flip()
I don't see an image on the window and I have no idea what I'm doing wrong, I don't get any error message.
You need to draw the image before you flip the window:
showingimage.draw()
window.flip()
Otherwise it will show a blank screen. By the way, you need not put the filename in parentheses.

Categories