forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpil_pixels.py
More file actions
33 lines (25 loc) · 773 Bytes
/
pil_pixels.py
File metadata and controls
33 lines (25 loc) · 773 Bytes
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
# coding: utf-8
"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
PIL examples to play with pixels.
"""
import mss
from PIL import Image
with mss.mss() as sct:
# Get a screenshot of the 1st monitor
sct_img = sct.grab(sct.monitors[1])
# Create an Image
img = Image.new("RGB", sct_img.size)
# Best solution: create a list(tuple(R, G, B), ...) for putdata()
pixels = zip(sct_img.raw[2::4], sct_img.raw[1::4], sct_img.raw[0::4])
img.putdata(list(pixels))
# But you can set individual pixels too (slower)
"""
pixels = img.load()
for x in range(sct_img.width):
for y in range(sct_img.height):
pixels[x, y] = sct_img.pixel(x, y)
"""
# Show it!
img.show()