How can I get the logo/icon of an app with path - python

import os
path = r'C:\Users\John\AppData\Roaming\Spotify\Spotify.exe'
os.startfile(path)
What I want, in this situation for example we have Spotify, is to save the logo somewhere based on its path.
I want to use it for a tkinter app I have built.
Maybe something in the idea of:
my_image = get_image_from_path(file=r'C:\Users\John\AppData\Roaming\Spotify\Spotify.exe')

This draws an icon from a given executable into a bitmap, and saves the bitmap.
My web searching to answer this question brought up a thread on the pywin32 mailing list from 2009. I don't remember writing it, but this is the code from my reply:
import win32ui
import win32gui
import win32con
import win32api
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)
large, small = win32gui.ExtractIconEx("c:/windows/system32/shell32.dll",0)
win32gui.DestroyIcon(large[0])
hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) )
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap( hdc, ico_x, ico_y )
hdc = hdc.CreateCompatibleDC()
hdc.SelectObject( hbmp )
hdc.DrawIcon( (0,0), small[0] )
hbmp.SaveBitmapFile( hdc, "save.bmp" )

Related

In Python, how do I make a specific window stay on top?

I have seen a few methods, but none of the work.
import win32gui, win32process, win32con
import os
windowList = []
win32gui.EnumWindows(lambda hwnd, windowList: windowList.append((win32gui.GetWindowText(hwnd),hwnd)), windowList)
cmdWindow = [i for i in windowList if "c:\python26\python.exe" in i[0].lower()]
win32gui.SetWindowPos(cmdWindow[0][1],win32con.HWND_TOPMOST,0,0,100,100,0) #100,100 is the size of the window
'''
This one doens't work because the range exceeds the list.
'''
import win32gui
import win32con
hwnd = win32gui.GetForegroundWindow()
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)
This one works as in it stays on top, but not the window I want.
Is there a line I could add so it knows which window I want? Or is there another way to approach this?
Use win32gui.FindWindow
hwnd = win32gui.FindWindow(None, 'Untitled - Notepad')
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)

Disable resizing for python console application in Windows

Here is a code to disable resizing using c++
How to change console window style at runtime?
HWND consoleWindow = GetConsoleWindow();
SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
However, I want to use this code in python. It's my effort so far:
def fix_borders():
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
hWnd = kernel32.GetConsoleWindow()
kernel32.SetWindowLong(hWnd, GWL_STYLE,
kernel32.GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX)
However, SetWindowLong isn't recognized and I don't know how to import other constants...
This is how I solved it still utilizing ctypes and using win32api (definitely not the most elegant solution though):
def lock_resize():
while True:
STDOUT = -11
hdl = windll.kernel32.GetStdHandle(STDOUT)
rect = ct.wintypes.SMALL_RECT(0, 50, 50, 80) # (left, top, right, bottom)
windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
_thread.start_new_thread(lock_resize,()) #using the _thread module to keep everything else running

How to get the screenshot of a tkinter window

I am trying to build a program which gets me an enlarged photo of the text I want, for this I decided to use tkinter, win32gui and pygetwindow modules after taking some tips from already asked problems on stack overflow am having the following problems:
(1)I don't know how to get the hwnd value of the tkinter window which I created.
(2)I can't get hwnd value even if I know how to get it as the window is created after the complete code has run.
So please suggest me solutions to the problem
This is my code:
from tkinter import *
import win32gui
import pygetwindow as gw
#making the tkinter window
root = Tk()
root.title('DaysLeft')
#getting all the windows with their hwnd values
hwnd=gw.getAllWindows()
print(hwnd)
win32gui.SetForegroundWindow(hwnd)
bbox = win32gui.GetWindowRect(hwnd)
img = ImageGrab.grab(bbox)
img.show()
mainloop()
The above code gives error below as expected:.
line 26, in <module>
win32gui.SetForegroundWindow(hwnd)
TypeError: The object is not a PyHANDLE object
You can use PIL for taking a screenshot and win32gui or pygetwindow to get windows location.
Install PIL by saying
pip install Pillow
then your working code would be:
from tkinter import *
from win32gui import FindWindow, GetWindowRect
import pygetwindow as gw
from PIL import ImageGrab
def ss():
win = gw.getWindowsWithTitle('DaysLeft')[0]
winleft = win.left+9
wintop = win.top+38 #change 38 to 7 to not capture the titlebar
winright = win.right-9
winbottom = win.bottom-9
final_rect = (winleft,wintop,winright,winbottom)
img = ImageGrab.grab(final_rect)
img.save('Required Image.png')
#making the tkinter window
root = Tk()
root.title('DaysLeft')
root.after(3000,ss)
root.mainloop()
Why am i subtracting some amount from the pixels? its because, windows has decorations like drop shadow effect to the windows, which are also part of the windows and will be included in the screenshot, so i used this to get rid of those extra pixels.
Or if your still reluctant on using win32gui then, change the function to:
from win32gui import FindWindow, GetWindowRect
from PIL import ImageGrab
......
def ss():
win = FindWindow(None, 'DaysLeft')
rect = GetWindowRect(win)
list_rect = list(rect)
list_frame = [-9, -38, 9, 9] #change -38 to -7 to not capture the titlebar
final_rect = tuple((map(lambda x,y:x-y,list_rect,list_frame))) #subtracting two lists
img = ImageGrab.grab(bbox=final_rect)
img.save('Image.png')
What is after method? It just calls the function after 3000 ms, i.e, 3 seconds. We are basically giving the system some time to build the GUI and capture screenshot.
Hope it helped, do let me know if any errors or doubts.
Cheers

Comparing cursor icon bitmaps in python

I want to check whether the cursor icon has changed by comparing the bitmaps.
So far I have tried the snippet below, but it does not work properly.
import win32api, win32con, win32gui, win32ui
info = win32gui.GetCursorInfo()
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, 35, 35)
hdc = hdc.CreateCompatibleDC()
hdc.DrawIcon((0,0), info[1])
hbmp.SaveBitmapFile(hdc, 'icon.bmp')
This code just produces a black rectangle bitmap (found most of it on the internet). In general I would rather not save the bitmap and just compare the 2 bitmaps as images with pillow, but i don't know how to do that.
After you have created the memory DC and the memory bitmap, SelectObject is used to select the memory bitmap into the memory DC. Only in this way can the bitmap work.
Modified codeļ¼š
import win32api, win32con, win32gui, win32ui
info = win32gui.GetCursorInfo()
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, 35, 35)
hdc = hdc.CreateCompatibleDC()
hdc.SelectObject(hbmp)
hdc.DrawIcon((0,0), info[1])
hbmp.SaveBitmapFile(hdc, 'icon.bmp')
win32gui.DestroyIcon(info[1])
win32gui.DeleteObject(hbmp.GetHandle())
hdc.DeleteDC()
When you no longer need the bitmap, call the DeleteObject function to delete it.
Similar operations apply to cursor and memory DC release.

How can I close an image shown to the user with the Python Imaging Library?

I have several images which I would like to show the user with Python. The user should enter some description and then the next image should be shown.
This is my code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, glob
from PIL import Image
path = '/home/moose/my/path/'
for infile in glob.glob( os.path.join(path, '*.png') ):
im = Image.open(infile)
im.show()
value = raw_input("Description: ")
# store and do some other stuff. Now the image-window should get closed
It is working, but the user has to close the image himself. Could I get python to close the image after the description has been entered?
I don't need PIL. If you have another idea with another library / bash-program (with subprocess), it'll be also fine.
psutil can get the pid of the display process created by im.show() and kill the process with that pid on every operating system:
import time
import psutil
from PIL import Image
# open and show image
im = Image.open('myImageFile.jpg')
im.show()
# display image for 10 seconds
time.sleep(10)
# hide image
for proc in psutil.process_iter():
if proc.name() == "display":
proc.kill()
The show method "is mainly intended for debugging purposes" and spawns an external process for which you don't get a handle, so you can't kill it in a proper way.
With PIL, you may want to use one of its GUI modules , such as ImageTk, ImageQt or ImageWin.
Otherwise, just manually spawn an image viewer from Python with the subprocess module:
for infile in glob.glob( os.path.join(path, '*.png')):
viewer = subprocess.Popen(['some_viewer', infile])
viewer.terminate()
viewer.kill() # make sure the viewer is gone; not needed on Windows
I've modified this recipe before to do some image work in Python. It uses Tkinter, so it doesn't require any modules besides PIL.
'''This will simply go through each file in the current directory and
try to display it. If the file is not an image then it will be skipped.
Click on the image display window to go to the next image.
Noah Spurrier 2007'''
import os, sys
import Tkinter
import Image, ImageTk
def button_click_exit_mainloop (event):
event.widget.quit() # this will cause mainloop to unblock.
root = Tkinter.Tk()
root.bind("<Button>", button_click_exit_mainloop)
root.geometry('+%d+%d' % (100,100))
dirlist = os.listdir('.')
old_label_image = None
for f in dirlist:
try:
image1 = Image.open(f)
root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
tkpi = ImageTk.PhotoImage(image1)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
root.title(f)
if old_label_image is not None:
old_label_image.destroy()
old_label_image = label_image
root.mainloop() # wait until user clicks the window
except Exception, e:
# This is used to skip anything not an image.
# Warning, this will hide other errors as well.
pass

Categories

Resources