Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcdbb56

Browse files
author
Mickaël Schoentgen
committed
New example: how to capture only a part of the screen
1 parent 52bdf0a commit bcdbb56

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

docs/source/examples.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,20 @@ This is an example using `frombytes() <http://pillow.readthedocs.io/en/latest/re
130130

131131
# And save it!
132132
img.save('monitor-{0}.jpg'.format(num))
133+
134+
135+
Part of the screen
136+
==================
137+
138+
You can capture only a part of the screen::
139+
140+
from mss import mss
141+
142+
143+
with mss() as sct:
144+
# The screen part to capture
145+
mon = {'top': 160, 'left': 160, 'width': 222, 'height': 42}
146+
147+
# Create the screen part
148+
output = 'sct-{top}x{left}_{width}x{height}.png'.format(**mon)
149+
sct.to_png(sct.get_pixels(mon), output)

examples/part-of-screen.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
''' This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
5+
'''
6+
7+
from mss.exception import ScreenshotError
8+
from mss import mss
9+
10+
11+
def main():
12+
# type: () -> int
13+
''' Example to capture part of the screen. '''
14+
15+
try:
16+
with mss() as sct:
17+
# The screen part to capture
18+
mon = {'top': 160, 'left': 160, 'width': 160, 'height': 135}
19+
20+
# Create the picture
21+
output = 'sct-{top}x{left}_{width}x{height}.png'.format(**mon)
22+
sct.to_png(sct.get_pixels(mon), output)
23+
print(output)
24+
25+
return 0
26+
except ScreenshotError as ex:
27+
print(ex)
28+
29+
return 1
30+
31+
32+
if __name__ == '__main__':
33+
exit(main())

0 commit comments

Comments
 (0)