python 剪切板截个图
2022/1/11 9:04:19
本文主要是介绍python 剪切板截个图,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import ctypes from ctypes.wintypes import * import win32clipboard from win32con import * import sys class BITMAPFILEHEADER(ctypes.Structure): _pack_ = 1 # structure field byte alignment _fields_ = [ ('bfType', WORD), # file type ("BM") ('bfSize', DWORD), # file size in bytes ('bfReserved1', WORD), # must be zero ('bfReserved2', WORD), # must be zero ('bfOffBits', DWORD), # byte offset to the pixel array ] SIZEOF_BITMAPFILEHEADER = ctypes.sizeof(BITMAPFILEHEADER) class BITMAPINFOHEADER(ctypes.Structure): _pack_ = 1 # structure field byte alignment _fields_ = [ ('biSize', DWORD), ('biWidth', LONG), ('biHeight', LONG), ('biPLanes', WORD), ('biBitCount', WORD), ('biCompression', DWORD), ('biSizeImage', DWORD), ('biXPelsPerMeter', LONG), ('biYPelsPerMeter', LONG), ('biClrUsed', DWORD), ('biClrImportant', DWORD) ] SIZEOF_BITMAPINFOHEADER = ctypes.sizeof(BITMAPINFOHEADER) win32clipboard.OpenClipboard() try: if win32clipboard.IsClipboardFormatAvailable(win32clipboard.CF_DIB): data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB) else: print('clipboard does not contain an image in DIB format') sys.exit(1) finally: win32clipboard.CloseClipboard() bmih = BITMAPINFOHEADER() ctypes.memmove(ctypes.pointer(bmih), data, SIZEOF_BITMAPINFOHEADER) if bmih.biCompression != BI_BITFIELDS: # RGBA? print('insupported compression type {}'.format(bmih.biCompression)) sys.exit(1) bmfh = BITMAPFILEHEADER() ctypes.memset(ctypes.pointer(bmfh), 0, SIZEOF_BITMAPFILEHEADER) # zero structure bmfh.bfType = ord('B') | (ord('M') << 8) bmfh.bfSize = SIZEOF_BITMAPFILEHEADER + len(data) # file size SIZEOF_COLORTABLE = 0 bmfh.bfOffBits = SIZEOF_BITMAPFILEHEADER + SIZEOF_BITMAPINFOHEADER + SIZEOF_COLORTABLE bmp_filename = 'clipboard.bmp' with open(bmp_filename, 'wb') as bmp_file: bmp_file.write(bmfh) bmp_file.write(data) print('file "{}" created from clipboard image'.format(bmp_filename))
这篇关于python 剪切板截个图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04Python外包有哪些常见的主要库-icode9专业技术文章分享
- 2024-10-02基于深度学习的钢铁缺陷检测系统(yolov8、注意力机制、PyQt5界面、Python)
- 2024-10-01怎么用python搭建一个系统-icode9专业技术文章分享
- 2024-09-30Python基础入门教程
- 2024-09-30获取参数学习:Python中的基础教程
- 2024-09-30Python编程基础详解
- 2024-09-29点击加载学习:Python编程基础教程
- 2024-09-29数据科学五大Python前端库:第二部分
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础