How to make python see a particular window? - python-3.x

I am using python 3, where i am using ImageGrab to see the portion of the screen.
The thing I want to do is get the full application window may be it of any size that is the program will get all the window that I want.
My Code Is:
from PIL import ImageGrab
import numpy as np
import cv2
while(True):
img = ImageGrab.grab(bbox=(0,0,580,580))
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
#frame = img_np
cv2.imshow("test", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
I am getting this:
But I want this.The Full Window:
I also want to grab the window anywhere on the screen.

Related

Display video size on label tkinter

The below code displays video on the label. But, problem is that, it is displayed in a very zoom (large) manner. I want to resize it to display correctly on label. When I use the option image=image.resize(), I get an error
ValueError: cannot resize this array: it does not own its data
import tkinter as tk, threading
import imageio
from PIL import Image, ImageTk
video_name = "e.mp4"
video = imageio.get_reader(video_name)
#video = video.resize(20,20)
def stream(label):
for image in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(image))
label.config(image=frame_image)
label.image = frame_image
root = tk.Tk()
my_label = tk.Label(root, width=500,height=500)
my_label.place(x=0,y=0)
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.start()
root.mainloop()
You can call resize() on the return image of Image.fromarray(image):
frame_image = ImageTk.PhotoImage(Image.fromarray(image).resize((100,100)))

How to integrates or call GUI script (gui.py) to source script (main.py)?

what kind of module, syntax, function or do i need to use classes (oop)? i am still a baby python3, opencv4....please help
gui.py
'''
from guizero import App, PushButton
def do_nothing()
print('Button was pressed')
app=App()
button=PushButton(app,command=do_nothing)
app.display()
'''
main.py
'''
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while(True):
ret, frame = cap.read()
frame = cv2.flip(frame, -1) # Flip camera vertically
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', frame)
cv2.imshow('gray', gray)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
'''
right now, i just wanted to have simple ui...by just pressing ''(button1), the main.py will pop-up
You can't just pop main.py. What you can do is put the OpenCV code in a function that you will call from your PushButton instance i.e. replace do_nothing we a function that does something similar. Note that I don't know much about guizero so I can't tell what will happen once you start your infinite loop.
Side notes:
There might already be something in guizero to show picture so you don't have to use imshow.
OpenCV already provides the highgui module which might be enough for your purpose.

How to show image without title bar and border in opencv python

Show image like its shows in full screen but in small size. from backend we select the image size and we have to show this image without any title bar and border. only image will appear. i try lot of methods, but didn't get success. is there any way to do this ? if any body knows, please help, i stuck in this from many days
Operating System: Raspberry PI
Language Using: Python 3
With the help of tkinter window/frame, we can show image or video without any border and title bar. what we have to do is, make tkinter window transparent.
I am putting my code below, which shows webcam video without borders and title bar in any size.
import numpy as np
import cv2
from tkinter import *
#import tkinter as tk
from PIL import Image, ImageTk
import sys
window = Tk() #Makes main window
window.overrideredirect(True)
window.wm_attributes("-topmost", True)
window.geometry("+600+200")
display1 = Label(window)
display1.grid(row=1, column=0, padx=0, pady=0) #Display 1
cap = cv2.VideoCapture(0)
def show_frame():
_, frame = cap.read()
frame = cv2.resize(frame, (400,400))
#frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(master = display1, image=img)
display1.imgtk = imgtk #Shows frame for display 1
display1.configure(image=imgtk)
window.after(10, show_frame)
show_frame()
window.mainloop()
frame = cv2.resize(frame, (400,400)) this is use to resize our frame.
Windows where images are displayed in OpenCV are implemented in HighGUI module using either OpenGL or Qt. You can modify a bit what will be shown to the user (depends on system also), but i think what you're trying to achive is not possible in OpenCV.
Here is list of all flags you can use while creating new window/widget in OpenCV.
Unfortunately there is no flag to hide title bar like it is in full-screen mode. Why? It's not important to OpenCV users. They are using imshow just to see the results, mainly for debugging purposes, so they don't need to remove title-bars.
What you can do? Link your program with Qt framework and display image using Qt directly, not throught the wrapper. This might be helpful.

Detect a key pressed in dlib.image_window()

Having the following code:
import dlib
import cv2
from lib.capture import Capture
win = dlib.image_window()
cap = Capture() # Capture image from webcam
cap.start()
while(True):
frame = cap.get() # Get current frame from webcam
if frame is not None:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Converting from RGB to BGR (as dlib.image_window requires)
win.set_image(frame) # Display the resulting frame
How can I detect a key pressed e.g. "ESC" in the dlib window???
You can use this:
from msvcrt import getch
import dlib
import cv2
from lib.capture import Capture
win = dlib.image_window()
cap = Capture() # Capture image from webcam
cap.start()
while(True):
frame = cap.get() # Get current frame from webcam
if frame is not None:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Converting from RGB to BGR (as dlib.image_window requires)
win.set_image(frame) # Display the resulting frame
key = ord(getch())
if key == 27: #ESC
break

Plot mouse clicks over an image

I writing a code in Python 3 to plot some markers over a DICOM image. for this, I wrote a very short program:
In the main program, I read the DICOM filename from the terminal and plot the image.
main_prog.py:
import sys
import dicom as dcm
import numpy as np
from matplotlib import pyplot as plt
from dicomplot import dicomplot as dcmplot
filename = sys.argv[1]
dicomfile = dcm.read_file(filename)
dicomimg = dicomfile.pixel_array
fig = plt.figure(dpi = 300)
ax = fig.add_subplot(1, 1, 1)
plt.set_cmap(plt.gray())
plt.pcolormesh(np.flipud(dicomimg))
dcm = dcmplot(ax)
plt.show()
Then, I define a class to store the coordinates clicked by the user and plot each of them at a time over the image:
dicomplot.py
from matplotlib import pyplot as plt
class dicomplot():
def __init__(self, img):
self.img = img
self.fig = plt.figure(dpi = 300)
self.xcoord = list()
self.ycoord = list()
self.cid = img.figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
if event.button == 1:
self.xcoord.append(event.x)
self.ycoord.append(event.y)
self.img.plot(self.ycoord, self.xcoord, 'r*')
self.img.figure.canvas.draw()
elif event.button == 2:
self.img.figure.canvas.mpl_disconnect(self.cid)
elif event.button == 3:
self.xcoord.append(-1)
self.ycoord.append(-1)
The problem is that when I click over the image, the markers appear in a different scale, and not over the image as they are supposed to.
How can I modify my code so when I click on the image, all the mouse clicks are stored and ploted in the desired position?
The MouseEvent objects carry both a x/y andxdata/ydata attributes (docs). The first set is in screen coordinates (ex pixels from the lower left) and the second set (*data) are in the data coordinates.
You might also be interested in mpldatacursor.

Resources