XyperCode
XyperCode

Reputation: 15

How to capture image behind window in python (tkinter)

I like to capture pixels behind the window, blur them with the Pillow package and finally display them as image on a frame.
I can't find anything but an unanswered question: Python Tkinter get image of whats behind a window

Is it possible to do it in Python 3.7?

Here is an example how the code will be:

import tkinter as tk
from PIL import Image, ImageTk


class MainWindow(tk.Tk):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.overrideredirect(True)
        self.frame = tk.Frame(self, relief="flat", bd=0)

        self.label = tk.Label()

    def mainloop(self, n=0):
        while self.winfo_exists():
            background = Image.frombytes("RGB", ..., ...)
            tk_backkground = ImageTk.PhotoImage(background)

            self.label.config(image=tk_backkground)
            self.update()
            self.update_idletasks()


if __name__ == '__main__':
    root = MainWindow()
    root.mainloop()

if you need more information, just say it.

(Sorry for my bad English)

Upvotes: 0

Views: 572

Answers (1)

Pedro
Pedro

Reputation: 380

in windows you can use BlurWindow

python -m pip install BlurWindow

from tkinter import *
from ctypes import windll

from BlurWindow.blurWindow import blur

root = Tk()
root.config(bg='green')

root.wm_attributes("-transparent", 'green')
root.geometry('500x400')

root.update()

hWnd = windll.user32.GetForegroundWindow()
blur(hWnd)



def color(hex):
    hWnd = windll.user32.GetForegroundWindow()
    blur(hWnd,hexColor=hex)
    

e = Entry(width=9)
e.insert(0,'#12121240')

e.pack()
b = Button(text='Apply',command=lambda:[color(e.get())])
b.pack()


root.mainloop()

win11

Upvotes: 2

Related Questions