Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@
)


def test_sanity(tmp_path: Path) -> None:
def roundtrip(im: Image.Image) -> None:
outfile = 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"
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
def test_sanity(mode: str, tmp_path: Path) -> None:
outfile = tmp_path / "temp.bmp"

roundtrip(hopper())
im = hopper(mode)
im.save(outfile, "BMP")

roundtrip(hopper("1"))
roundtrip(hopper("L"))
roundtrip(hopper("P"))
roundtrip(hopper("RGB"))
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"


def test_invalid_file() -> None:
Expand Down
28 changes: 15 additions & 13 deletions Tests/test_file_sgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,26 @@ def test_invalid_file() -> None:
SgiImagePlugin.SgiImageFile(invalid_file)


def test_write(tmp_path: Path) -> None:
def roundtrip(img: Image.Image) -> None:
out = tmp_path / "temp.sgi"
img.save(out, format="sgi")
def roundtrip(img: Image.Image, tmp_path: Path) -> None:
out = tmp_path / "temp.sgi"
img.save(out, format="sgi")
assert_image_equal_tofile(img, out)

out = tmp_path / "fp.sgi"
with open(out, "wb") as fp:
img.save(fp)
assert_image_equal_tofile(img, out)

out = tmp_path / "fp.sgi"
with open(out, "wb") as fp:
img.save(fp)
assert_image_equal_tofile(img, out)
assert not fp.closed


assert not fp.closed
@pytest.mark.parametrize("mode", ("L", "RGB", "RGBA"))
def test_write(mode: str, tmp_path: Path) -> None:
roundtrip(hopper(mode), tmp_path)

for mode in ("L", "RGB", "RGBA"):
roundtrip(hopper(mode))

# Test 1 dimension for an L mode image
roundtrip(Image.new("L", (10, 1)))
def test_write_L_mode_1_dimension(tmp_path: Path) -> None:
roundtrip(Image.new("L", (10, 1)), tmp_path)


def test_write16(tmp_path: Path) -> None:
Expand Down
68 changes: 36 additions & 32 deletions Tests/test_file_tga.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

import os
from glob import glob
from itertools import product
from pathlib import Path

import pytest
Expand All @@ -15,14 +13,27 @@
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")


_MODES = ("L", "LA", "P", "RGB", "RGBA")
_ORIGINS = ("tl", "bl")

_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}


@pytest.mark.parametrize("mode", _MODES)
def test_sanity(mode: str, tmp_path: Path) -> None:
@pytest.mark.parametrize(
"size_mode",
(
("1x1", "L"),
("200x32", "L"),
("200x32", "LA"),
("200x32", "P"),
("200x32", "RGB"),
("200x32", "RGBA"),
),
)
@pytest.mark.parametrize("origin", _ORIGINS)
@pytest.mark.parametrize("rle", (True, False))
def test_sanity(
size_mode: tuple[str, str], origin: str, rle: str, tmp_path: Path
) -> None:
def roundtrip(original_im: Image.Image) -> None:
out = tmp_path / "temp.tga"

Expand All @@ -36,33 +47,26 @@ def roundtrip(original_im: Image.Image) -> None:

assert_image_equal(saved_im, original_im)

png_paths = glob(os.path.join(_TGA_DIR_COMMON, f"*x*_{mode.lower()}.png"))

for png_path in png_paths:
with Image.open(png_path) as reference_im:
assert reference_im.mode == mode

path_no_ext = os.path.splitext(png_path)[0]
for origin, rle in product(_ORIGINS, (True, False)):
tga_path = "{}_{}_{}.tga".format(
path_no_ext, origin, "rle" if rle else "raw"
)

with Image.open(tga_path) as original_im:
assert original_im.format == "TGA"
assert original_im.get_format_mimetype() == "image/x-tga"
if rle:
assert original_im.info["compression"] == "tga_rle"
assert (
original_im.info["orientation"]
== _ORIGIN_TO_ORIENTATION[origin]
)
if mode == "P":
assert original_im.getpalette() == reference_im.getpalette()

assert_image_equal(original_im, reference_im)

roundtrip(original_im)
size, mode = size_mode
png_path = os.path.join(_TGA_DIR_COMMON, size + "_" + mode.lower() + ".png")
with Image.open(png_path) as reference_im:
assert reference_im.mode == mode

path_no_ext = os.path.splitext(png_path)[0]
tga_path = "{}_{}_{}.tga".format(path_no_ext, origin, "rle" if rle else "raw")

with Image.open(tga_path) as original_im:
assert original_im.format == "TGA"
assert original_im.get_format_mimetype() == "image/x-tga"
if rle:
assert original_im.info["compression"] == "tga_rle"
assert original_im.info["orientation"] == _ORIGIN_TO_ORIENTATION[origin]
if mode == "P":
assert original_im.getpalette() == reference_im.getpalette()

assert_image_equal(original_im, reference_im)

roundtrip(original_im)


def test_palette_depth_8() -> None:
Expand Down
Loading