forked from python-pillow/Pillow
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_file_bmp.py
139 lines (99 loc) · 3.79 KB
/
test_file_bmp.py
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
import io
import pytest
from PIL import BmpImagePlugin, Image
from .helper import assert_image_equal, hopper
def test_sanity(tmp_path):
def roundtrip(im):
outfile = str(tmp_path / "temp.bmp")
im.save(outfile, "BMP")
with Image.open(outfile) as reloaded:
reloaded.load()
assert im.mode == reloaded.mode
assert im.size == reloaded.size
assert reloaded.format == "BMP"
assert reloaded.get_format_mimetype() == "image/bmp"
roundtrip(hopper())
roundtrip(hopper("1"))
roundtrip(hopper("L"))
roundtrip(hopper("P"))
roundtrip(hopper("RGB"))
def test_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp:
with pytest.raises(SyntaxError):
BmpImagePlugin.BmpImageFile(fp)
def test_save_to_bytes():
output = io.BytesIO()
im = hopper()
im.save(output, "BMP")
output.seek(0)
with Image.open(output) as reloaded:
assert im.mode == reloaded.mode
assert im.size == reloaded.size
assert reloaded.format == "BMP"
def test_save_too_large(tmp_path):
outfile = str(tmp_path / "temp.bmp")
with Image.new("RGB", (1, 1)) as im:
im._size = (37838, 37838)
with pytest.raises(ValueError):
im.save(outfile)
def test_dpi():
dpi = (72, 72)
output = io.BytesIO()
with hopper() as im:
im.save(output, "BMP", dpi=dpi)
output.seek(0)
with Image.open(output) as reloaded:
assert reloaded.info["dpi"] == dpi
def test_save_bmp_with_dpi(tmp_path):
# Test for #1301
# Arrange
outfile = str(tmp_path / "temp.jpg")
with Image.open("Tests/images/hopper.bmp") as im:
# Act
im.save(outfile, "JPEG", dpi=im.info["dpi"])
# Assert
with Image.open(outfile) as reloaded:
reloaded.load()
assert im.info["dpi"] == reloaded.info["dpi"]
assert im.size == reloaded.size
assert reloaded.format == "JPEG"
def test_load_dpi_rounding():
# Round up
with Image.open("Tests/images/hopper.bmp") as im:
assert im.info["dpi"] == (96, 96)
# Round down
with Image.open("Tests/images/hopper_roundDown.bmp") as im:
assert im.info["dpi"] == (72, 72)
def test_save_dpi_rounding(tmp_path):
outfile = str(tmp_path / "temp.bmp")
with Image.open("Tests/images/hopper.bmp") as im:
im.save(outfile, dpi=(72.2, 72.2))
with Image.open(outfile) as reloaded:
assert reloaded.info["dpi"] == (72, 72)
im.save(outfile, dpi=(72.8, 72.8))
with Image.open(outfile) as reloaded:
assert reloaded.info["dpi"] == (73, 73)
def test_load_dib():
# test for #1293, Imagegrab returning Unsupported Bitfields Format
with Image.open("Tests/images/clipboard.dib") as im:
assert im.format == "DIB"
assert im.get_format_mimetype() == "image/bmp"
with Image.open("Tests/images/clipboard_target.png") as target:
assert_image_equal(im, target)
def test_save_dib(tmp_path):
outfile = str(tmp_path / "temp.dib")
with Image.open("Tests/images/clipboard.dib") as im:
im.save(outfile)
with Image.open(outfile) as reloaded:
assert reloaded.format == "DIB"
assert reloaded.get_format_mimetype() == "image/bmp"
assert_image_equal(im, reloaded)
def test_rgba_bitfields():
# This test image has been manually hexedited
# to change the bitfield compression in the header from XBGR to RGBA
with Image.open("Tests/images/rgb32bf-rgba.bmp") as im:
# So before the comparing the image, swap the channels
b, g, r = im.split()[1:]
im = Image.merge("RGB", (r, g, b))
with Image.open("Tests/images/bmp/q/rgb32bf-xbgr.bmp") as target:
assert_image_equal(im, target)