自动截图软件2025专业版.py# 调整区域,上方下移45像素,下方上移80像素 adjusted_region = (0, 45, screen_width, screen_height - 45 - 80)

import tkinter as tk
from tkinter import messagebox
import pyautogui
import time
import os
from PIL import Image

全局变量

screenshot_interval = 3 # 截图间隔时间(3秒),最小为1秒,最大为60秒
is_running = False
screenshot_count = 0
start_time = 0
selected_area = None # 截图区域存储变量
folder_name = "BiDuiTu-Z" # 截图保存目录(BiDuiTu-Z)不能用中文,否则用比对程序时会不识别并报错的
selection_overlay = None
dragging = False
start_x, start_y = 0, 0
resize_direction = None

def select_region():
global selection_overlay, dragging, start_x, start_y, resize_direction
root = tk.Tk()
root.attributes('-fullscreen', True)
root.attributes('-alpha', 0.3)
root.attributes('-topmost', True)
canvas = tk.Canvas(root, cursor="cross", highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=True)

rect = None
left, top, right, bottom = 0, 0, 0, 0

def on_press(event):
    global dragging, start_x, start_y, resize_direction
    start_x, start_y = event.x, event.y
    nonlocal rect, left, top, right, bottom
    if rect is None:
        rect = canvas.create_rectangle(start_x, start_y, start_x, start_y, outline='red', width=2)
        left, top, right, bottom = start_x, start_y, start_x, start_y
    else:
        # 检查是否在边框附近,以确定是否为调整大小操作
        x1, y1, x2, y2 = canvas.coords(rect)
        border_width = 5
        if abs(event.x - x1) < border_width and abs(event.y - y1) < border_width:
            resize_direction = 'top-left'
        elif abs(event.x - x2) < border_width and abs(event.y - y1) < border_width:
            resize_direction = 'top-right'
        elif abs(event.x - x1) < border_width and abs(event.y - y2) < border_width:
            resize_direction = 'bottom-left'
        elif abs(event.x - x2) < border_width and abs(event.y - y2) < border_width:
            resize_direction = 'bottom-right'
        elif abs(event.x - x1) < border_width:
            resize_direction = 'left'
        elif abs(event.x - x2) < border_width:
            resize_direction = 'right'
        elif abs(event.y - y1) < border_width:
            resize_direction = 'top'
        elif abs(event.y - y2) < border_width:
            resize_direction = 'bottom'
        else:
            dragging = True

def on_drag(event):
    global dragging, start_x, start_y, resize_direction
    end_x, end_y = event.x, event.y
    nonlocal rect, left, top, right, bottom
    if rect is not None:
        if dragging:
            dx = end_x - start_x
            dy = end_y - start_y
            left += dx
            top += dy
            right += dx
            bottom += dy
            canvas.coords(rect, left, top, right, bottom)
            start_x, start_y = end_x, end_y
        elif resize_direction:
            if resize_direction == 'left':
                left = min(end_x, right)
            elif resize_direction == 'right':
                right = max(end_x, left)
            elif resize_direction == 'top':
                top = min(end_y, bottom)
            elif resize_direction == 'bottom':
                bottom = max(end_y, top)
            elif resize_direction == 'top-left':
                left = min(end_x, right)
                top = min(end_y, bottom)
            elif resize_direction == 'top-right':
                right = max(end_x, left)
                top = min(end_y, bottom)
            elif resize_direction == 'bottom-left':
                left = min(end_x, right)
                bottom = max(end_y, top)
            elif resize_direction == 'bottom-right':
                right = max(end_x, left)
                bottom = max(end_y, top)
            canvas.coords(rect, left, top, right, bottom)
        else:
            # 初始绘制矩形框
            left = min(start_x, end_x)
            top = min(start_y, end_y)
            right = max(start_x, end_x)
            bottom = max(start_y, end_y)
            canvas.coords(rect, left, top, right, bottom)

def on_release(event):
    global dragging, resize_direction
    dragging = False
    resize_direction = None
    nonlocal left, top, right, bottom
    root.destroy()
    global selected_area
    selected_area = (min(left, right), min(top, bottom), abs(right - left), abs(bottom - top))

    # 创建一个半透明的顶层窗口来显示所选区域
    def create_overlay():
        global selection_overlay
        if selection_overlay:
            selection_overlay.destroy()
        selection_overlay = tk.Toplevel()
        selection_overlay.attributes('-alpha', 0.3)
        selection_overlay.attributes('-topmost', True)
        # 让窗口接收鼠标事件
        selection_overlay.attributes('-disabled', False)
        selection_overlay.overrideredirect(True)
        overlay_canvas = tk.Canvas(selection_overlay, highlightthickness=0)
        overlay_canvas.pack(fill=tk.BOTH, expand=True)
        overlay_rect = overlay_canvas.create_rectangle(0, 0, abs(right - left), abs(bottom - top), outline='red',
                                                       width=2)

        def overlay_on_press(event):
            nonlocal left, top, right, bottom
            global dragging, start_x, start_y, resize_direction
            start_x, start_y = event.x, event.y
            x1, y1, x2, y2 = overlay_canvas.coords(overlay_rect)
            border_width = 5
            if abs(event.x - 0) < border_width and abs(event.y - 0) < border_width:
                resize_direction = 'top-left'
            elif abs(event.x - (right - left)) < border_width and abs(event.y - 0) < border_width:
                resize_direction = 'top-right'
            elif abs(event.x - 0) < border_width and abs(event.y - (bottom - top)) < border_width:
                resize_direction = 'bottom-left'
            elif abs(event.x - (right - left)) < border_width and abs(event.y - (bottom - top)) < border_width:
                resize_direction = 'bottom-right'
            elif abs(event.x - 0) < border_width:
                resize_direction = 'left'
            elif abs(event.x - (right - left)) < border_width:
                resize_direction = 'right'
            elif abs(event.y - 0) < border_width:
                resize_direction = 'top'
            elif abs(event.y - (bottom - top)) < border_width:
                resize_direction = 'bottom'
            else:
                dragging = True

        def overlay_on_drag(event):
            nonlocal left, top, right, bottom
            global dragging, start_x, start_y, resize_direction
            end_x, end_y = event.x, event.y
            if dragging:
                dx = end_x - start_x
                dy = end_y - start_y
                left += dx
                top += dy
                right += dx
                bottom += dy
                overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                start_x, start_y = end_x, end_y
            elif resize_direction:
                if resize_direction == 'left':
                    new_left = left + (end_x - start_x)
                    if new_left < right:
                        left = new_left
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'right':
                    new_right = left + end_x
                    if new_right > left:
                        right = new_right
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'top':
                    new_top = top + (end_y - start_y)
                    if new_top < bottom:
                        top = new_top
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'bottom':
                    new_bottom = top + end_y
                    if new_bottom > top:
                        bottom = new_bottom
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'top-left':
                    new_left = left + (end_x - start_x)
                    new_top = top + (end_y - start_y)
                    if new_left < right and new_top < bottom:
                        left = new_left
                        top = new_top
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'top-right':
                    new_right = left + end_x
                    new_top = top + (end_y - start_y)
                    if new_right > left and new_top < bottom:
                        right = new_right
                        top = new_top
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'bottom-left':
                    new_left = left + (end_x - start_x)
                    new_bottom = top + end_y
                    if new_left < right and new_bottom > top:
                        left = new_left
                        bottom = new_bottom
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                elif resize_direction == 'bottom-right':
                    new_right = left + end_x
                    new_bottom = top + end_y
                    if new_right > left and new_bottom > top:
                        right = new_right
                        bottom = new_bottom
                        overlay_canvas.coords(overlay_rect, 0, 0, right - left, bottom - top)
                        selection_overlay.geometry(f"{right - left}x{bottom - top}+{left}+{top}")
                start_x, start_y = end_x, end_y

        def overlay_on_release(event):
            nonlocal left, top, right, bottom
            global dragging, resize_direction
            dragging = False
            resize_direction = None
            global selected_area
            selected_area = (left, top, right - left, bottom - top)

        def overlay_on_motion(event):
            nonlocal left, top, right, bottom
            x, y = event.x, event.y
            x1, y1, x2, y2 = overlay_canvas.coords(overlay_rect)
            border_width = 5
            if abs(x - 0) < border_width and abs(y - 0) < border_width:
                overlay_canvas.config(cursor="top_left_corner")
            elif abs(x - (right - left)) < border_width and abs(y - 0) < border_width:
                overlay_canvas.config(cursor="top_right_corner")
            elif abs(x - 0) < border_width and abs(y - (bottom - top)) < border_width:
                overlay_canvas.config(cursor="bottom_left_corner")
            elif abs(x - (right - left)) < border_width and abs(y - (bottom - top)) < border_width:
                overlay_canvas.config(cursor="bottom_right_corner")
            elif abs(x - 0) < border_width:
                overlay_canvas.config(cursor="sb_h_double_arrow")
            elif abs(x - (right - left)) < border_width:
                overlay_canvas.config(cursor="sb_h_double_arrow")
            elif abs(y - 0) < border_width:
                overlay_canvas.config(cursor="sb_v_double_arrow")
            elif abs(y - (bottom - top)) < border_width:
                overlay_canvas.config(cursor="sb_v_double_arrow")
            else:
                overlay_canvas.config(cursor="fleur")

        overlay_canvas.bind('<ButtonPress-1>', overlay_on_press)
        overlay_canvas.bind('<B1-Motion>', overlay_on_drag)
        overlay_canvas.bind('<ButtonRelease-1>', overlay_on_release)
        overlay_canvas.bind('<Motion>', overlay_on_motion)

        # 设置窗口大小和位置
        selection_overlay.geometry(f"{abs(right - left)}x{abs(bottom - top)}+{min(left, right)}+{min(top, bottom)}")

    # 使用 after 方法在事件循环空闲时创建窗口
    root.after(100, create_overlay)

canvas.bind('<ButtonPress-1>', on_press)
canvas.bind('<B1-Motion>', on_drag)
canvas.bind('<ButtonRelease-1>', on_release)

# 添加提示文字
tip_text = "鼠标拖拽选择截图区域\n(ESC退出)"
canvas.create_text(root.winfo_screenwidth() // 2, 50,
                   text=tip_text, font=('Arial', 24), fill='white')

root.bind('<Escape>', lambda e: root.destroy())
root.mainloop()
return selected_area

def select_screenshot_area():
global selected_area
try:
# selected_area = pyautogui.select()
selected_area = select_region()
messagebox.showinfo("信息", "截图区域已选择")
except Exception as e:
messagebox.showerror("错误", f"选择区域时出错: {e}")

def start_screenshot():
global is_running, start_time, screenshot_count
if not os.path.exists(folder_name):
os.makedirs(folder_name)
is_running = True
start_time = time.time()
if selection_overlay:
selection_overlay.destroy()
take_screenshot()

def take_screenshot():
global is_running, screenshot_count
if is_running:
current_time = time.strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"{folder_name}/{current_time}.jpg"
try:
if selected_area:
left, top, width, height = selected_area
screenshot = pyautogui.screenshot(region=(left, top, width, height))
# 按实际尺寸保存截图区域的截图
screenshot = screenshot.convert("RGB")
screenshot.save(file_name, "JPEG", quality=93, dpi=(96, 96))
else:
screen_width, screen_height = pyautogui.size()
# 调整区域,上方下移45像素,下方上移80像素
adjusted_region = (0, 45, screen_width, screen_height - 45 - 80)
screenshot = pyautogui.screenshot(region=adjusted_region)
screenshot = screenshot.resize((1920, 956))
screenshot = screenshot.convert("RGB")
screenshot.save(file_name, "JPEG", quality=93, dpi=(96, 96))
screenshot_count += 1
count_label.config(text=f"截图数量: {screenshot_count} 张")
elapsed_time = (time.time() - start_time) / 60
time_label.config(text=f"截图已进行: {elapsed_time:.2f} 分钟")
except Exception as e:
messagebox.showerror("错误", f"截图时出错: {e}")
root.after(screenshot_interval * 1000, take_screenshot)

def pause_screenshot():
global is_running
is_running = False

def stop_screenshot():
global is_running, screenshot_count, start_time
is_running = False
screenshot_count = 0
start_time = 0
count_label.config(text="截图数量: 0 张")
time_label.config(text="截图已进行: 0.00 分钟")
if selection_overlay:
selection_overlay.destroy()

def increase_interval():
global screenshot_interval
if screenshot_interval < 60:
screenshot_interval += 1
interval_label.config(text=f"间隔: {screenshot_interval} 秒")

def decrease_interval():
global screenshot_interval
if screenshot_interval > 1:
screenshot_interval -= 1
interval_label.config(text=f"间隔: {screenshot_interval} 秒")

创建界面主窗口

root = tk.Tk()
root.title("自动截图2025专业版")

获取屏幕宽度和高度

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

计算窗口宽度和高度

window_width = 300
window_height = 200

计算窗口的位置

x = (screen_width - window_width) // 2

上移五行,假设一行高度约为20像素,最下面一行为0时,上移5行就是100

y = screen_height - window_height - 100

设置窗口的位置

root.geometry(f"{window_width}x{window_height}+{x}+{y}")

第一行按钮

button_frame1 = tk.Frame(root)
button_frame1.pack(pady=10)

select_area_button = tk.Button(button_frame1, text="截图区域", command=select_screenshot_area)
select_area_button.pack(side=tk.LEFT, padx=12)

full_screen_button = tk.Button(button_frame1, text="全屏截图", command=lambda: setattr(root, 'selected_area', None))
full_screen_button.pack(side=tk.LEFT, padx=12)

decrease_button = tk.Button(button_frame1, text="-", command=decrease_interval)
decrease_button.pack(side=tk.LEFT, padx=5)

interval_label = tk.Label(button_frame1, text=f"间隔: {screenshot_interval} 秒")
interval_label.pack(side=tk.LEFT, padx=5)

increase_button = tk.Button(button_frame1, text="+", command=increase_interval)
increase_button.pack(side=tk.LEFT, padx=5)

第二行按钮 数字padX=15指按钮间的间距

button_frame2 = tk.Frame(root)
button_frame2.pack(pady=10)

start_button = tk.Button(button_frame2, text="开始", command=start_screenshot)
start_button.pack(side=tk.LEFT, padx=15)

pause_button = tk.Button(button_frame2, text="暂停", command=pause_screenshot)
pause_button.pack(side=tk.LEFT, padx=15)

stop_button = tk.Button(button_frame2, text="停止", command=stop_screenshot)
stop_button.pack(side=tk.LEFT, padx=15)

计数器和时间显示器

count_label = tk.Label(root, text="截图数量: 0 张")
count_label.pack(pady=5)

time_label = tk.Label(root, text="截图已进行: 0.00 分钟")
time_label.pack(pady=5)

运行主循环

root.mainloop()

posted @ 2025-05-13 22:00  寻龙三迹  阅读(33)  评论(0)    收藏  举报