File: test_pyscreeze.py

package info (click to toggle)
pyscreeze 0.0~git20240820225245.93f2775-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,988 kB
  • sloc: python: 820; makefile: 146
file content (309 lines) | stat: -rw-r--r-- 13,108 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import unittest
import sys
import os
import pyscreeze

scriptFolder = os.path.dirname(os.path.realpath(__file__))
# Delete PIL.py, which is made by test_pillow_unavailable.py, just in case it
# was left over from some incomplete run of that test.
if os.path.exists(os.path.join(scriptFolder, 'PIL.py')):
    os.unlink(os.path.join(scriptFolder, 'PIL.py'))

from PIL import Image


# Change the cwd to this file's folder, because that's where the test image files are located.
scriptFolder = os.path.dirname(os.path.realpath(__file__))
os.chdir(scriptFolder)

RUNNING_ON_PYTHON_2 = sys.version_info[0] == 2
TEMP_FILENAME = '_delete_me.png'

# On Linux, figure out which window system is being used.
if sys.platform.startswith('linux'):
    RUNNING_X11 = False
    RUNNING_WAYLAND = False
    if os.environ.get('XDG_SESSION_TYPE') == 'x11':
        RUNNING_X11 = True
        RUNNING_WAYLAND = False
    elif os.environ.get('XDG_SESSION_TYPE') == 'wayland':
        RUNNING_WAYLAND = True
        RUNNING_X11 = False
    elif 'WAYLAND_DISPLAY' in os.environ:
        RUNNING_WAYLAND = True
        RUNNING_X11 = False
    elif 'DISPLAY' in os.environ:
        RUNNING_WAYLAND = False
        RUNNING_X11 = True

# Helper functions to get current screen resolution on Windows, Mac OS X, or Linux.
# Non-Windows platforms require additional modules:
#   OS X: sudo pip3 install pyobjc-core
#         sudo pip3 install pyobjc
#   Linux: sudo pip3 install python3-Xlib
def resolutionOSX():
    return Quartz.CGDisplayPixelsWide(0), Quartz.CGDisplayPixelsHigh(0)

def resolutionX11():
    return _display.screen().width_in_pixels, _display.screen().height_in_pixels

def resolutionWayland():
    xrandrOutput = subprocess.run(['xrandr'], shell=True, capture_output=True, text=True).stdout
    mo = re.search(r'current (\d+) x (\d+)', xrandrOutput)
    if mo is None:
        raise Exception('xrandr output does not list the wayland resolution. xrandr output:\n' + xrandrOutput)
    return int(mo.group(1)), int(mo.group(2))

def resolutionWin32():
    return (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))

# Assign the resolution() function to the function appropriate for the platform.
if sys.platform == 'darwin':
    import Quartz
    resolution = resolutionOSX
elif sys.platform == 'win32':
    import ctypes
    resolution = resolutionWin32
elif sys.platform.startswith('linux'):
    if RUNNING_X11:
        from Xlib.display import Display
        _display = Display(None)
        resolution = resolutionX11
    elif RUNNING_WAYLAND:
        import subprocess
        import re
        resolution = resolutionWayland
else:
    assert False, 'PyScreeze is not supported on platform ' + sys.platform


# PNG file format magic numbers are 89 50 4E 47 0d 0a 1a 0a
pngMagicNumbers = [137, 80, 78, 71, 13, 10, 26, 10]
def isPng(filename):
    fp = open(filename, 'rb')
    fileMagicNumbers = fp.read(len(pngMagicNumbers))
    fp.close()
    if RUNNING_ON_PYTHON_2:
        return fileMagicNumbers == bytearray(pngMagicNumbers)
    else:
        return fileMagicNumbers == bytes(pngMagicNumbers)


# JPG file format magic numbres are FF D8 FF
jpgMagicNumbers = [255, 216, 255]
def isJpg(filename):
    fp = open(filename, 'rb')
    fileMagicNumbers = fp.read(len(jpgMagicNumbers))
    fp.close()
    if RUNNING_ON_PYTHON_2:
        return fileMagicNumbers == bytearray(jpgMagicNumbers)
    else:
        return fileMagicNumbers == bytes(jpgMagicNumbers)


class TestMagicNumbers(unittest.TestCase):
    def test_pngMagicNumbers(self):
        # Testing my test helper function to make sure it correctly
        # identifies PNG files.
        self.assertTrue(isPng('largenoise.png'))
        self.assertTrue(isPng('colornoise.png'))
        self.assertTrue(isPng('haystack1.png'))
        self.assertTrue(isPng('haystack2.png'))
        self.assertTrue(isPng('slash.png'))
        self.assertTrue(isPng('zophie.png'))
        self.assertTrue(isPng('zophie_face.png'))
        self.assertFalse(isPng('zophie.jpg'))
        self.assertFalse(isPng(__file__))

    def test_jpgMagicNumbers(self):
        # Testing my test helper function to make sure it correctly
        # identifies JPG files.
        self.assertFalse(isJpg('largenoise.png'))
        self.assertFalse(isJpg('colornoise.png'))
        self.assertFalse(isJpg('haystack1.png'))
        self.assertFalse(isJpg('haystack2.png'))
        self.assertFalse(isJpg('slash.png'))
        self.assertFalse(isJpg('zophie.png'))
        self.assertFalse(isJpg('zophie_face.png'))
        self.assertTrue(isJpg('zophie.jpg'))
        self.assertFalse(isJpg(__file__))

class TestGeneral(unittest.TestCase):
    def test_namesDefined(self):
        pyscreeze.locateAll
        pyscreeze.locate
        pyscreeze.locateOnScreen
        pyscreeze.locateAllOnScreen
        pyscreeze.locateCenterOnScreen
        pyscreeze.center
        pyscreeze.pixelMatchesColor
        pyscreeze.pixel


    @unittest.skip("flaky test")
    def test_screenshot(self):
        im = pyscreeze.screenshot(TEMP_FILENAME)
        self.assertTrue(isPng(TEMP_FILENAME))
        self.assertEqual(im.size, resolution()) # TODO shouldn't this fail on Windows for multi-monitor setups?
        os.unlink(TEMP_FILENAME)


    @unittest.skip("flaky test")
    def test_screenshot_regions(self):
        im = pyscreeze.screenshot(TEMP_FILENAME, region=(0, 0, 100, 150))
        self.assertEqual(im.size, (100, 150))
        os.unlink(TEMP_FILENAME)

        im = pyscreeze.screenshot(TEMP_FILENAME, region=(50, 50, 100, 150))
        self.assertEqual(im.size, (100, 150))
        os.unlink(TEMP_FILENAME)


    # TODO - lots of warnings about unclosed file handles for these tests.
    def test_locate_filename(self):
        self.assertEqual((94, 94, 4, 4), tuple(pyscreeze.locate('slash.png', 'haystack1.png')))
        self.assertEqual((93, 93, 4, 4), tuple(pyscreeze.locate('slash.png', 'haystack2.png')))

        self.assertEqual((94, 94, 4, 4), tuple(pyscreeze.locate('slash.png', 'haystack1.png', grayscale=True)))
        self.assertEqual((93, 93, 4, 4), tuple(pyscreeze.locate('slash.png', 'haystack2.png', grayscale=True)))

        pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = True
        with self.assertRaises(pyscreeze.ImageNotFoundException):
            pyscreeze.locate('slash.png', 'colornoise.png')
        with self.assertRaises(pyscreeze.ImageNotFoundException):
            pyscreeze.locate('slash.png', 'colornoise.png', grayscale=True)

        pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = False
        self.assertEqual(pyscreeze.locate('slash.png', 'colornoise.png'), None)
        self.assertEqual(pyscreeze.locate('slash.png', 'colornoise.png', grayscale=True), None)

    def test_locate_im(self):
        slashFp = open('slash.png' ,'rb')
        haystack1Fp = open('haystack1.png' ,'rb')
        haystack2Fp = open('haystack2.png' ,'rb')
        colorNoiseFp = open('colornoise.png' ,'rb')
        slashIm = Image.open(slashFp)
        haystack1Im = Image.open(haystack1Fp)
        haystack2Im = Image.open(haystack2Fp)
        colorNoiseIm = Image.open(colorNoiseFp)

        self.assertEqual((94, 94, 4, 4), tuple(pyscreeze.locate(slashIm, haystack1Im)))
        self.assertEqual((93, 93, 4, 4), tuple(pyscreeze.locate(slashIm, haystack2Im)))

        self.assertEqual((94, 94, 4, 4), tuple(pyscreeze.locate(slashIm, haystack1Im, grayscale=True)))
        self.assertEqual((93, 93, 4, 4), tuple(pyscreeze.locate(slashIm, haystack2Im, grayscale=True)))

        pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = True
        with self.assertRaises(pyscreeze.ImageNotFoundException):
            pyscreeze.locate(slashIm, colorNoiseIm)
        with self.assertRaises(pyscreeze.ImageNotFoundException):
            pyscreeze.locate(slashIm, colorNoiseIm, grayscale=True)

        pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = False
        self.assertEqual(pyscreeze.locate(slashIm, colorNoiseIm), None)
        self.assertEqual(pyscreeze.locate(slashIm, colorNoiseIm, grayscale=True), None)

        slashFp.close()
        haystack1Fp.close()
        haystack2Fp.close()
        colorNoiseFp.close()

    def test_locateAll_filename(self):
        self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll('slash.png', 'haystack1.png')))
        self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll('slash.png', 'haystack2.png')))

        self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll('slash.png', 'haystack1.png', grayscale=True)))
        self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll('slash.png', 'haystack2.png', grayscale=True)))

        self.assertEqual((), tuple(pyscreeze.locateAll('slash.png', 'colornoise.png')))
        self.assertEqual((), tuple(pyscreeze.locateAll('slash.png', 'colornoise.png', grayscale=True)))

    def test_locateAll_im(self):
        slashFp = open('slash.png' ,'rb')
        haystack1Fp = open('haystack1.png' ,'rb')
        haystack2Fp = open('haystack2.png' ,'rb')
        colorNoiseFp = open('colornoise.png' ,'rb')
        slashIm = Image.open(slashFp)
        haystack1Im = Image.open(haystack1Fp)
        haystack2Im = Image.open(haystack2Fp)
        colorNoiseIm = Image.open(colorNoiseFp)

        self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll(slashIm, haystack1Im)))
        self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll(slashIm, haystack2Im)))

        self.assertEqual(((94, 94, 4, 4),), tuple(pyscreeze.locateAll(slashIm, haystack1Im, grayscale=True)))
        self.assertEqual(((93, 93, 4, 4), (94, 94, 4, 4), (95, 95, 4, 4)), tuple(pyscreeze.locateAll(slashIm, haystack2Im, grayscale=True)))

        self.assertEqual((), tuple(pyscreeze.locateAll(slashIm, colorNoiseIm)))
        self.assertEqual((), tuple(pyscreeze.locateAll(slashIm, colorNoiseIm, grayscale=True)))

        slashFp.close()
        haystack1Fp.close()
        haystack2Fp.close()
        colorNoiseFp.close()

    def test_imageNotFound(self):
        colorNoiseFp = open('colornoise.png' ,'rb')
        colorNoiseIm = Image.open(colorNoiseFp)
        slashFp = open('slash.png' ,'rb')
        slashIm = Image.open(slashFp)

        pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = True
        with self.assertRaises(pyscreeze.ImageNotFoundException):
            pyscreeze.locate(slashIm, colorNoiseIm)

        pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION = False
        self.assertEqual(pyscreeze.locate(slashIm, colorNoiseIm), None)

        colorNoiseFp.close()
        slashFp.close()

    def test_center(self):
        self.assertEqual((10, 10), pyscreeze.center((0, 0, 20, 20)))
        self.assertEqual((10, 10), pyscreeze.center((5, 5, 10, 10)))

        self.assertEqual((100, 100), pyscreeze.center((0, 0, 200, 200)))
        self.assertEqual((100, 100), pyscreeze.center((50, 50, 100, 100)))

    """
    # Disabling step test; we don't use this feature because it does not bring any significant performance improvement.
    def test_locate_im_step(self):
        slashFp = open('slash.png' ,'rb')
        haystack1Fp = open('haystack1.png' ,'rb')
        haystack2Fp = open('haystack2.png' ,'rb')
        colorNoiseFp = open('colornoise.png' ,'rb')
        slashIm = Image.open(slashFp)
        haystack1Im = Image.open(haystack1Fp)
        haystack2Im = Image.open(haystack2Fp)
        colorNoiseIm = Image.open(colorNoiseFp)

        for step in range(1, 10):
            self.assertEqual(((94, 94, 4, 4), step), (tuple(pyscreeze.locate(slashIm, haystack1Im, step=step)), step))
            self.assertEqual(((93, 93, 4, 4), step), (tuple(pyscreeze.locate(slashIm, haystack2Im, step=step)), step))

            self.assertEqual(((94, 94, 4, 4), step), (tuple(pyscreeze.locate(slashIm, haystack1Im, grayscale=True, step=step)), step))
            self.assertEqual(((93, 93, 4, 4), step), (tuple(pyscreeze.locate(slashIm, haystack2Im, grayscale=True, step=step)), step))

            self.assertEqual((None, step), (pyscreeze.locate(slashIm, colorNoiseIm, step=step), step))
            self.assertEqual((None, step), (pyscreeze.locate(slashIm, colorNoiseIm, grayscale=True, step=step), step))

        slashFp.close()
        haystack1Fp.close()
        haystack2Fp.close()
        colorNoiseFp.close()
        """

class TestStressTest(unittest.TestCase):
    def test_1000screenshots(self):
        # This test takes about two minutes for 200 screenshots.
        # On Windows, if I change PyScreeze away from Pillow and make win32 api calls directly but forget to release
        # the DCs (display contexts), the program would fail after about 90 or screenshots.
        # https://stackoverflow.com/questions/3586046/fastest-way-to-take-a-screenshot-with-python-on-windows
        if sys.platform == 'win32':
            for i in range(200):
                pyscreeze.screenshot(TEMP_FILENAME)
                self.assertTrue(isPng(TEMP_FILENAME))
                os.unlink(TEMP_FILENAME)

if __name__ == '__main__':
    unittest.main()