-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscreenshot.py
More file actions
124 lines (100 loc) · 3.6 KB
/
Copy pathscreenshot.py
File metadata and controls
124 lines (100 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding:utf-8 -*-
from ctypes import windll
from .winConst import WinConst
import win32gui, win32ui
from struct import pack
import collections
import zlib
user32=windll.user32
def to_png(data, size, level=6, file_name=None):
width, height = size
line = width * 3
png_filter = pack(">B", 0)
scanlines = b"".join(
[png_filter + data[y * line : y * line + line] for y in range(height)]
)
magic = pack(">8B", 137, 80, 78, 71, 13, 10, 26, 10)
ihdr = [b"", b"IHDR", b"", b""]
ihdr[2] = pack(">2I5B", width, height, 8, 2, 0, 0, 0)
ihdr[3] = pack(">I", zlib.crc32(b"".join(ihdr[1:3])) & 0xFFFFFFFF)
ihdr[0] = pack(">I", len(ihdr[2]))
idat = [b"", b"IDAT", zlib.compress(scanlines, level), b""]
idat[3] = pack(">I", zlib.crc32(b"".join(idat[1:3])) & 0xFFFFFFFF)
idat[0] = pack(">I", len(idat[2]))
iend = [b"", b"IEND", b"", b""]
iend[3] = pack(">I", zlib.crc32(iend[1]) & 0xFFFFFFFF)
iend[0] = pack(">I", len(iend[2]))
if not file_name:
return magic + b"".join(ihdr + idat + iend)
with open(file_name, "wb") as f:
f.write(magic+ b"".join(ihdr + idat + iend))
return None
class pixelsParse:
__slots__ = {"__rgb", "pos", "raw", "size"}
def __init__(self, data, left=0,top=0,width=0,height=0, size=None):
_Pos = collections.namedtuple("Pos", "left, top")
_Size = collections.namedtuple("Size", "width, height")
self.__rgb = None
self.raw = data
self.pos = _Pos(left, top)
if size is not None:
self.size = size
else:
self.size = _Size(width, height)
@property
def height(self):
return self.size.height
@property
def left(self):
return self.pos.left
@property
def rgb(self):
if not self.__rgb:
rgb = bytearray(self.height * self.width * 3)
raw = self.raw
rgb[0::3] = raw[2::4]
rgb[1::3] = raw[1::4]
rgb[2::3] = raw[0::4]
self.__rgb = bytes(rgb)
return self.__rgb
@property
def top(self):
return self.pos.top
@property
def width(self):
return self.size.width
def generate_bitBmp(hwnd=0,screenDC=0,left=0,top=0,width=0,height=0,cx=0,cy=0):
if screenDC==0:
screenDC = user32.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(screenDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC,width,height)
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0,0), (width,height), mfcDC, (0, 0), WinConst.SRCCOPY)
pixels = saveBitMap.GetBitmapBits(True)
img=pixelsParse(data=bytearray(pixels), left=left
,top=top,width=width,height=height)
pixels=None
return img
def screen_shot(file_name,hwnd=0,screenDC=0,left=0,top=0,width=0,height=0):
if not all([width,height]):return False
img=generate_bitBmp(hwnd=hwnd, screenDC=screenDC,left=left,top=top,width=width,height=height,cx=0,cy=0)
to_png(img.rgb, img.size, level=6, file_name=file_name)
img=None
return True
def whole_screenshot(mb,webview,file_name):
left=0
top=0
width=mb.wkeGetContentWidth(webview)
height=mb.wkeGetContentHeight(webview)
pixels=create_string_buffer(width*height*4)
mb.wkeResize(webview,width,height)
mb.wkeUpdate()
mb.wkePaint(webview,byref(pixels),0)
img=pixelsParse(data=bytearray(pixels), left=left
,top=top,width=width,height=height)
to_png(img.rgb, img.size, level=6, file_name=file_name)
pixels=None
img=None
return True