加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test_imagegrab.py 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
LLL 提交于 2022-06-27 15:20 . Import Upstream version 7.0.0
import subprocess
import sys
from .helper import PillowTestCase
try:
from PIL import ImageGrab
class TestImageGrab(PillowTestCase):
def test_grab(self):
for im in [
ImageGrab.grab(),
ImageGrab.grab(include_layered_windows=True),
ImageGrab.grab(all_screens=True),
]:
self.assert_image(im, im.mode, im.size)
def test_grabclipboard(self):
if sys.platform == "darwin":
subprocess.call(["screencapture", "-cx"])
else:
p = subprocess.Popen(
["powershell", "-command", "-"], stdin=subprocess.PIPE
)
p.stdin.write(
b"""[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$bmp = New-Object Drawing.Bitmap 200, 200
[Windows.Forms.Clipboard]::SetImage($bmp)"""
)
p.communicate()
im = ImageGrab.grabclipboard()
self.assert_image(im, im.mode, im.size)
except ImportError:
class TestImageGrab(PillowTestCase):
def test_skip(self):
self.skipTest("ImportError")
class TestImageGrabImport(PillowTestCase):
def test_import(self):
# Arrange
exception = None
# Act
try:
from PIL import ImageGrab
ImageGrab.__name__ # dummy to prevent Pyflakes warning
except Exception as e:
exception = e
# Assert
if sys.platform in ["win32", "darwin"]:
self.assertIsNone(exception)
else:
self.assertIsInstance(exception, ImportError)
self.assertEqual(str(exception), "ImageGrab is macOS and Windows only")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化