好友
阅读权限10
听众
最后登录1970-1-1
|
无聊搞了个, 电脑屏幕截图gif 图片的功能, 新手实现,大佬勿喷,
win32clipboard 库默认python3.8 版本, 其他版本可能又报错默认注释,
添加该库默认为了保存文件后自动复制到剪贴板
[Python] 纯文本查看 复制代码 import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QFileDialog, QInputDialog
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QPainter, QPen, QColor
from PIL import Image
import mss
# import win32clipboard
from io import BytesIO
class ScreenshotTool(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Screenshot Tool')
self.setGeometry(300, 300, 300, 200)
layout = QVBoxLayout()
self.label = QLabel('Click and drag to select area')
layout.addWidget(self.label)
self.button = QPushButton('Start Screenshot', self)
self.button.clicked.connect(self.start_screenshot)
layout.addWidget(self.button)
self.setLayout(layout)
def start_screenshot(self):
duration, ok = QInputDialog.getInt(self, "Capture Duration", "Enter capture duration in seconds:", 5, 1, 60)
if ok:
self.hide() # Hide the main window
self.screenshot_widget = ScreenshotWidget(duration)
self.screenshot_widget.show()
class ScreenshotWidget(QWidget):
def __init__(self, duration):
super().__init__()
self.duration = duration
self.setGeometry(0, 0, QApplication.desktop().screenGeometry().width(),
QApplication.desktop().screenGeometry().height())
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool)
self.setWindowOpacity(0.3)
self.begin = None
self.end = None
def paintEvent(self, event):
if self.begin and self.end:
qp = QPainter(self)
qp.setPen(QPen(QColor(255, 0, 0), 2, Qt.SolidLine))
qp.drawRect(QRect(self.begin, self.end))
def mousePressEvent(self, event):
self.begin = event.pos()
self.end = event.pos()
self.update()
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.hide()
x1 = min(self.begin.x(), self.end.x())
y1 = min(self.begin.y(), self.end.y())
x2 = max(self.begin.x(), self.end.x())
y2 = max(self.begin.y(), self.end.y())
self.capture_screen(x1, y1, x2 - x1, y2 - y1)
def capture_screen(self, x, y, width, height):
frames = []
fps = 10 # frames per second
frame_count = self.duration * fps
with mss.mss() as sct:
monitor = {"top": y, "left": x, "width": width, "height": height}
for _ in range(frame_count):
screenshot = sct.grab(monitor)
img = Image.frombytes("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX")
frames.append(img)
time.sleep(1 / fps)
file_path, _ = QFileDialog.getSaveFileName(self, "Save File", "", "GIF (*.gif)")
if file_path:
frames[0].save(file_path, save_all=True, append_images=frames[1:],
duration=1000 / fps, loop=0)
print(f"GIF saved to {file_path}")
self.copy_to_clipboard(file_path)
QApplication.quit()
def copy_to_clipboard(self, file_path):
image = Image.open(file_path)
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
# 文件复制到剪贴板
# win32clipboard.OpenClipboard()
# win32clipboard.EmptyClipboard()
# win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
# win32clipboard.CloseClipboard()
print("GIF copied to clipboard")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ScreenshotTool()
ex.show()
sys.exit(app.exec_())
|
-
开始选择区域
-
截取时常秒
免费评分
-
查看全部评分
|