Skip to content

Commit 24f0e2e

Browse files
committed
Add examples (PIL, GNU/Linux, general)
1 parent 0bba5d9 commit 24f0e2e

6 files changed

Lines changed: 66 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
build/
22
.cache/
33
dist/
4-
MANIFEST
4+
*.egg-info/
5+
MANIFEST*
56
.DS_Store
67
*.orig
78
*.png

MANIFEST.in

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/source/examples.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
Examples
33
========
44

5+
GNU/Linux
6+
---------
7+
8+
Usage example with a specific display::
9+
10+
from mss.linux import MSS
11+
12+
13+
display = ':0.0'
14+
print('Screenshot of display "{0}"'.format(display))
15+
output = 'monitor{0}-%d.png'.format(display)
16+
17+
with MSS(display=display) as screenshotter:
18+
for filename in screenshotter.save(output=output):
19+
print(filename)
20+
21+
522
Using PIL
623
---------
724

tests.py renamed to examples/callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from os import rename
88
from os.path import isfile
99

10-
from mss import ScreenshotError, mss
10+
from mss import mss, ScreenshotError
1111

1212

1313
def main():
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010

1111
def main():
12-
''' Usage example with specified display. '''
12+
''' Usage example with a specific display. '''
1313

1414
display = ':0.0'
15+
print('Screenshot of display "{0}"'.format(display))
16+
1517
try:
1618
with MSS(display=display) as screenshotter:
17-
print('Screenshot of display "{0}"'.format(display))
1819
output = 'monitor{0}-%d.png'.format(display)
1920
for filename in screenshotter.save(output=output):
2021
print(filename)

examples/pil.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 import mss, ScreenshotError
8+
from PIL import Image
9+
10+
11+
def main():
12+
''' PIL example using frombytes(). '''
13+
14+
try:
15+
with mss() as screenshotter:
16+
# We retrieve monitors informations:
17+
monitors = screenshotter.enum_display_monitors()
18+
19+
# Get rid of the first, as it represents the "All in One" monitor:
20+
for num, monitor in enumerate(monitors[1:], 1):
21+
# Get raw pixels from the screen.
22+
# This method will store screen size into `width` and `height`
23+
# and raw pixels into `image`.
24+
screenshotter.get_pixels(monitor)
25+
26+
# Create an Image:
27+
size = (screenshotter.width, screenshotter.height)
28+
img = Image.frombytes('RGB', size, screenshotter.image)
29+
30+
# And save it!
31+
output = 'monitor-{0}.png'.format(num)
32+
img.save(output)
33+
print(output)
34+
35+
return 0
36+
except ScreenshotError as ex:
37+
print(ex)
38+
39+
return 1
40+
41+
42+
if __name__ == '__main__':
43+
exit(main())

0 commit comments

Comments
 (0)