2019-10-07 16:28:36 +03:00
|
|
|
from io import BytesIO
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2022-02-26 09:29:21 +03:00
|
|
|
from PIL import Image, XbmImagePlugin
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
from .helper import hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
PIL151 = b"""
|
|
|
|
#define basic_width 32
|
|
|
|
#define basic_height 32
|
|
|
|
static char basic_bits[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x80, 0xff, 0xff, 0x01, 0x40, 0x00, 0x00, 0x02,
|
|
|
|
0x20, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x04, 0x10, 0x00, 0x00, 0x08,
|
|
|
|
0x10, 0x00, 0x00, 0x08,
|
|
|
|
0x10, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x08,
|
|
|
|
0x10, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x08,
|
|
|
|
0x20, 0x00, 0x00, 0x04,
|
|
|
|
0x20, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x02,
|
|
|
|
0x80, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
};
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_pil151():
|
|
|
|
with Image.open(BytesIO(PIL151)) as im:
|
|
|
|
im.load()
|
|
|
|
assert im.mode == "1"
|
|
|
|
assert im.size == (32, 32)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-05-12 12:43:58 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_open():
|
|
|
|
# Arrange
|
|
|
|
# Created with `convert hopper.png hopper.xbm`
|
|
|
|
filename = "Tests/images/hopper.xbm"
|
2015-05-12 12:43:58 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Act
|
|
|
|
with Image.open(filename) as im:
|
|
|
|
# Assert
|
|
|
|
assert im.mode == "1"
|
|
|
|
assert im.size == (128, 128)
|
2015-05-12 12:43:58 +03:00
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_open_filename_with_underscore():
|
|
|
|
# Arrange
|
|
|
|
# Created with `convert hopper.png hopper_underscore.xbm`
|
|
|
|
filename = "Tests/images/hopper_underscore.xbm"
|
2020-01-14 12:51:56 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
# Act
|
|
|
|
with Image.open(filename) as im:
|
|
|
|
# Assert
|
|
|
|
assert im.mode == "1"
|
|
|
|
assert im.size == (128, 128)
|
2020-01-14 13:10:10 +03:00
|
|
|
|
|
|
|
|
2022-02-26 09:29:21 +03:00
|
|
|
def test_invalid_file():
|
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
|
|
|
|
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
XbmImagePlugin.XbmImageFile(invalid_file)
|
|
|
|
|
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
def test_save_wrong_mode(tmp_path):
|
|
|
|
im = hopper()
|
|
|
|
out = str(tmp_path / "temp.xbm")
|
2020-01-14 13:10:10 +03:00
|
|
|
|
2020-01-27 14:46:52 +03:00
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.save(out)
|
|
|
|
|
|
|
|
|
|
|
|
def test_hotspot(tmp_path):
|
|
|
|
im = hopper("1")
|
|
|
|
out = str(tmp_path / "temp.xbm")
|
|
|
|
|
|
|
|
hotspot = (0, 7)
|
|
|
|
im.save(out, hotspot=hotspot)
|
|
|
|
|
|
|
|
with Image.open(out) as reloaded:
|
|
|
|
assert reloaded.info["hotspot"] == hotspot
|