Skip to content

Commit f436274

Browse files
Mickaël SchoentgenBoboTiG
authored andcommitted
Mac: handle screenshot's width not divisible by 16 (fix BoboTiG#19, BoboTiG#21)
1 parent e6cc1eb commit f436274

File tree

12 files changed

+69
-32
lines changed

12 files changed

+69
-32
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ History:
55
dev 2017/xx/xx
66
- add the 'Say Thanks' button
77
- doc: several fixes (fix #22)
8+
- Mac: handle screenshot's width not divisible by 16 (fix #14, #19, #21)
89

910
3.0.1 2017/07/06
1011
- fix examples links

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
# built documents.
6060
#
6161
# The short X.Y version.
62-
version = '3.0.2'
62+
version = '3.0.3'
6363
# The full version, including alpha/beta/rc tags.
6464
release = 'latest'
6565

mss/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .exception import ScreenShotError
1414
from .factory import mss
1515

16-
__version__ = '3.0.2'
16+
__version__ = '3.0.3'
1717
__author__ = "Mickaël 'Tiger-222' Schoentgen"
1818
__copyright__ = """
1919
Copyright (c) 2013-2017, Mickaël 'Tiger-222' Schoentgen

mss/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
2-
""" This is part of the MSS Python's module.
3-
Source: https://github.com/BoboTiG/python-mss
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
45
"""
56

67
from __future__ import print_function

mss/base.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
# coding: utf-8
22
"""
3-
mss.base
4-
~~~~~~~~
5-
6-
This module contains the basic classes that power MSS.
7-
8-
.. note::
9-
10-
The :class:`MSSBase <MSSBase>` class must be the parent class
11-
for every OS speicific implementation.
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
125
"""
136

147
import collections

mss/darwin.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# coding: utf-8
2-
""" This is part of the MSS Python's module.
3-
Source: https://github.com/BoboTiG/python-mss
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
45
"""
56

67
# pylint: disable=import-error
78

9+
from __future__ import division
10+
811
import ctypes
912
import ctypes.util
1013
import math
@@ -153,12 +156,13 @@ def grab(self, monitor):
153156
"""
154157
See :meth:`MSSBase.grab <mss.base.MSSBase.grab>` for full details.
155158
156-
When the monitor width is not divisible by 16, extra padding is
157-
added by macOS in the form of black pixels, which results
158-
in a screenshot with shifted pixels.
159+
When the monitor width is not divisible by 16, its width is reduced
160+
to the previous number divisible by 16. So we need to add extra
161+
black pixels.
159162
"""
160163

161-
rounded_width = math.ceil(monitor['width'] / 16) * 16
164+
rounded_width = int(math.ceil(monitor['width'] // 16) * 16)
165+
162166
rect = CGRect((monitor['left'], monitor['top']),
163167
(rounded_width, monitor['height']))
164168

@@ -177,8 +181,28 @@ def grab(self, monitor):
177181
data = data.contents
178182
self.core.CGDataProviderRelease(prov)
179183

180-
# The width is rounded to 16, so we can have resulting images with a
181-
# lesser width than requested.
182-
monitor['width'], monitor['height'] = width, height
184+
if rounded_width != monitor['width']:
185+
data = self.resize(data, monitor)
186+
187+
if len(data) != monitor['width'] * monitor['height'] * 4:
188+
del data
189+
raise ScreenShotError('Data length mismatch.', locals())
183190

184191
return ScreenShot(data, monitor)
192+
193+
@staticmethod
194+
def resize(data, monitor):
195+
# type: (bytearray, Dict[str, int]) -> bytearray
196+
""" Extend a 16 width-rounded screenshot to its original width. """
197+
198+
rounded_width = int(math.ceil(monitor['width'] // 16) * 16)
199+
cropped = bytearray()
200+
201+
for row in range(monitor['height']):
202+
start = row * rounded_width * 4
203+
end = start + monitor['width'] * 4
204+
cropped.extend(data[start:end])
205+
206+
cropped.extend(b'\00' * (monitor['width'] - rounded_width) * 4)
207+
208+
return cropped

mss/exception.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
2-
""" This is part of the MSS Python's module.
3-
Source: https://github.com/BoboTiG/python-mss
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
45
"""
56

67

mss/factory.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
2-
""" This is part of the MSS Python's module.
3-
Source: https://github.com/BoboTiG/python-mss
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
45
"""
56

67
import platform

mss/linux.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
2-
""" This is part of the MSS Python's module.
3-
Source: https://github.com/BoboTiG/python-mss
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
45
"""
56

67
import ctypes

mss/tools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
2-
""" This is part of the MSS Python's module.
3-
Source: https://github.com/BoboTiG/python-mss
2+
"""
3+
This is part of the MSS Python's module.
4+
Source: https://github.com/BoboTiG/python-mss
45
"""
56

67
import struct

0 commit comments

Comments
 (0)