我试着用Python.When创建了一个截图应用程序,我运行了它的功能,它的工作和裁剪的大小很好。但是,我使用tkinter创建GUI并运行,它有问题。屏幕被捕获并首先出现,但我无法将其裁剪成我想要的大小。在那之后,我输入'q',tkinter GUI出现,没有保存任何图像。有没有人可以帮我解决这个问题,以及如何选择文件来保存被捕获的图像?这是我的代码:
import cv2
import pyautogui
import tkinter as tk
from tkinter.filedialog import *
import os
import pyscreenshot as ImageGrab
from PIL import Image
import numpy as np
root = tk.Tk()
canvas1 = tk.Canvas(root, width=300, height=300)
canvas1.pack()
cropping = False
x_start, y_start, x_end, y_end = 0, 0, 0, 0
#event, x, y = 0
class takeScreenshot:
im1 = pyautogui.screenshot()
im1.save(r"monitor-1.png")
image = cv2.imread('monitor-1.png')
oriImage = image.copy()
try:
os.remove('monitor-1.png')
except:
pass
def mouse_crop(self,event, x, y, flags, param=None):
# grab references to the global variables
global x_start, y_start, x_end, y_end, cropping
# if the left mouse button was DOWN, start RECORDING
# (x, y) coordinates and indicate that cropping is being
if event == cv2.EVENT_LBUTTONDOWN:
x_start, y_start, x_end, y_end = x, y, x, y
cropping = True
# Mouse is Moving
elif event == cv2.EVENT_MOUSEMOVE:
if cropping:
x_end, y_end = x, y
# if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates
x_end, y_end = x, y
cropping = False # cropping is finished
refPoint = [(x_start, y_start), (x_end, y_end)]
if len(refPoint) == 2: # when two points were found
roi = self.oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0] [0]:refPoint[1][0]]
cv2.imwrite('d.png', roi)
cv2.namedWindow("image")
cv2.setMouseCallback("image", mouse_crop)
while True:
i = image.copy()
if not cropping:
cv2.imshow("image", image)
elif cropping:
cv2.rectangle(i, (x_start, y_start), (x_end, y_end), (255, 0, 0), 2)
cv2.imshow("image", i)
if cv2.waitKey(1) == ord('q'):
break
def close_window():
root.destroy() # destroying the main window
myQuitButton = tk.Button(text="Quit", command=close_window, font=10)
canvas1.create_window(50, 50, window=myQuitButton)
myButton = tk.Button(text="Take Screenshot", command=takeScreenshot, font=10)
canvas1.create_window(150, 150, window=myButton)
root.mainloop()发布于 2021-09-15 18:15:17
类主体中的代码在该类定义后立即调用。如果您希望在创建类的实例时调用代码,请添加__init__方法:
...
class takeScreenshot:
def __init__(self):
im1 = pyautogui.screenshot()
im1.save(r"monitor-1.png")
image = cv2.imread('monitor-1.png')
oriImage = image.copy()
try:
os.remove('monitor-1.png')
except:
pass
cv2.namedWindow("image")
cv2.setMouseCallback("image", self.mouse_crop)
while True:
i = image.copy()
if not cropping:
cv2.imshow("image", image)
elif cropping:
cv2.rectangle(i, (x_start, y_start), (x_end, y_end), (255, 0, 0), 2)
cv2.imshow("image", i)
if cv2.waitKey(1) == ord('q'):
break
def mouse_crop(self,event, x, y, flags, param=None):
# grab references to the global variables
global x_start, y_start, x_end, y_end, cropping
# if the left mouse button was DOWN, start RECORDING
# (x, y) coordinates and indicate that cropping is being
if event == cv2.EVENT_LBUTTONDOWN:
x_start, y_start, x_end, y_end = x, y, x, y
cropping = True
# Mouse is Moving
elif event == cv2.EVENT_MOUSEMOVE:
if cropping:
x_end, y_end = x, y
# if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates
x_end, y_end = x, y
cropping = False # cropping is finished
refPoint = [(x_start, y_start), (x_end, y_end)]
if len(refPoint) == 2: # when two points were found
roi = self.oriImage[refPoint[0][1]:refPoint[1][1], refPoint[0] [0]:refPoint[1][0]]
cv2.imwrite('d.png', roi)
... https://stackoverflow.com/questions/69197797
复制相似问题