Skip to content

Commit

Permalink
Removed DPI rounding when BMP loading
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 7, 2021
1 parent 9d72542 commit 0de3bea
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
Binary file removed Tests/images/hopper_roundDown.bmp
Binary file not shown.
27 changes: 7 additions & 20 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,46 +63,33 @@ def test_dpi():

output.seek(0)
with Image.open(output) as reloaded:
assert reloaded.info["dpi"] == dpi
assert reloaded.info["dpi"] == (72.008961115161, 72.008961115161)


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:
assert im.info["dpi"] == (95.98654816726399, 95.98654816726399)

# 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.info["dpi"] == (96, 96)
assert reloaded.size == im.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):
def test_save_float_dpi(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))
im.save(outfile, dpi=(72.21216100543306, 72.21216100543306))
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)
assert reloaded.info["dpi"] == (72.21216100543306, 72.21216100543306)


def test_load_dib():
Expand Down
4 changes: 1 addition & 3 deletions src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def _bitmap(self, header=0, offset=0):
)
file_info["colors"] = i32(header_data, 28)
file_info["palette_padding"] = 4
self.info["dpi"] = tuple(
int(x / 39.3701 + 0.5) for x in file_info["pixels_per_meter"]
)
self.info["dpi"] = tuple(x / 39.3701 for x in file_info["pixels_per_meter"])
if file_info["compression"] == self.BITFIELDS:
if len(header_data) >= 52:
for idx, mask in enumerate(
Expand Down

0 comments on commit 0de3bea

Please sign in to comment.