Pillow/Tests/test_file_xbm.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.3 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
from io import BytesIO
from pathlib import Path
2020-01-27 14:46:52 +03:00
import pytest
from PIL import Image, XbmImagePlugin
2020-01-27 14:46:52 +03:00
from .helper import hopper
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,
};
"""
def test_pil151() -> None:
2020-01-27 14:46:52 +03:00
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
def test_open() -> None:
2020-01-27 14:46:52 +03:00
# Arrange
# Created with `convert hopper.png hopper.xbm`
filename = "Tests/images/hopper.xbm"
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)
def test_open_filename_with_underscore() -> None:
2020-01-27 14:46:52 +03:00
# 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
def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
XbmImagePlugin.XbmImageFile(invalid_file)
def test_save_wrong_mode(tmp_path: Path) -> None:
2020-01-27 14:46:52 +03:00
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: Path) -> None:
2020-01-27 14:46:52 +03:00
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