Skip to content

Commit

Permalink
Merge pull request #5275 from radarhere/tofile
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Feb 21, 2021
2 parents 1857bf5 + 3495b31 commit 058b8d3
Show file tree
Hide file tree
Showing 36 changed files with 310 additions and 392 deletions.
11 changes: 4 additions & 7 deletions Tests/test_file_blp.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
from PIL import Image

from .helper import assert_image_equal
from .helper import assert_image_equal_tofile


def test_load_blp2_raw():
with Image.open("Tests/images/blp/blp2_raw.blp") as im:
with Image.open("Tests/images/blp/blp2_raw.png") as target:
assert_image_equal(im, target)
assert_image_equal_tofile(im, "Tests/images/blp/blp2_raw.png")


def test_load_blp2_dxt1():
with Image.open("Tests/images/blp/blp2_dxt1.blp") as im:
with Image.open("Tests/images/blp/blp2_dxt1.png") as target:
assert_image_equal(im, target)
assert_image_equal_tofile(im, "Tests/images/blp/blp2_dxt1.png")


def test_load_blp2_dxt1a():
with Image.open("Tests/images/blp/blp2_dxt1a.blp") as im:
with Image.open("Tests/images/blp/blp2_dxt1a.png") as target:
assert_image_equal(im, target)
assert_image_equal_tofile(im, "Tests/images/blp/blp2_dxt1a.png")
8 changes: 3 additions & 5 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from PIL import BmpImagePlugin, Image

from .helper import assert_image_equal, hopper
from .helper import assert_image_equal, assert_image_equal_tofile, hopper


def test_sanity(tmp_path):
Expand Down Expand Up @@ -111,8 +111,7 @@ def test_load_dib():
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)
assert_image_equal_tofile(im, "Tests/images/clipboard_target.png")


def test_save_dib(tmp_path):
Expand All @@ -136,5 +135,4 @@ def test_rgba_bitfields():
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)
assert_image_equal_tofile(im, "Tests/images/bmp/q/rgb32bf-xbgr.bmp")
43 changes: 19 additions & 24 deletions Tests/test_file_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from PIL import DdsImagePlugin, Image

from .helper import assert_image_equal
from .helper import assert_image_equal, assert_image_equal_tofile

TEST_FILE_DXT1 = "Tests/images/dxt1-rgb-4bbp-noalpha_MipMaps-1.dds"
TEST_FILE_DXT3 = "Tests/images/dxt3-argb-8bbp-explicitalpha_MipMaps-1.dds"
Expand Down Expand Up @@ -41,22 +41,20 @@ def test_sanity_dxt5():
assert im.mode == "RGBA"
assert im.size == (256, 256)

with Image.open(TEST_FILE_DXT5.replace(".dds", ".png")) as target:
assert_image_equal(target, im)
assert_image_equal_tofile(im, TEST_FILE_DXT5.replace(".dds", ".png"))


def test_sanity_dxt3():
"""Check DXT3 images can be opened"""

with Image.open(TEST_FILE_DXT3.replace(".dds", ".png")) as target:
with Image.open(TEST_FILE_DXT3) as im:
im.load()
with Image.open(TEST_FILE_DXT3) as im:
im.load()

assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)

assert_image_equal(target, im)
assert_image_equal_tofile(im, TEST_FILE_DXT3.replace(".dds", ".png"))


def test_dx10_bc7():
Expand All @@ -69,8 +67,7 @@ def test_dx10_bc7():
assert im.mode == "RGBA"
assert im.size == (256, 256)

with Image.open(TEST_FILE_DX10_BC7.replace(".dds", ".png")) as target:
assert_image_equal(target, im)
assert_image_equal_tofile(im, TEST_FILE_DX10_BC7.replace(".dds", ".png"))


def test_dx10_bc7_unorm_srgb():
Expand All @@ -84,10 +81,9 @@ def test_dx10_bc7_unorm_srgb():
assert im.size == (16, 16)
assert im.info["gamma"] == 1 / 2.2

with Image.open(
TEST_FILE_DX10_BC7_UNORM_SRGB.replace(".dds", ".png")
) as target:
assert_image_equal(target, im)
assert_image_equal_tofile(
im, TEST_FILE_DX10_BC7_UNORM_SRGB.replace(".dds", ".png")
)


def test_dx10_r8g8b8a8():
Expand All @@ -100,8 +96,7 @@ def test_dx10_r8g8b8a8():
assert im.mode == "RGBA"
assert im.size == (256, 256)

with Image.open(TEST_FILE_DX10_R8G8B8A8.replace(".dds", ".png")) as target:
assert_image_equal(target, im)
assert_image_equal_tofile(im, TEST_FILE_DX10_R8G8B8A8.replace(".dds", ".png"))


def test_dx10_r8g8b8a8_unorm_srgb():
Expand All @@ -115,10 +110,9 @@ def test_dx10_r8g8b8a8_unorm_srgb():
assert im.size == (16, 16)
assert im.info["gamma"] == 1 / 2.2

with Image.open(
TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB.replace(".dds", ".png")
) as target:
assert_image_equal(target, im)
assert_image_equal_tofile(
im, TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB.replace(".dds", ".png")
)


def test_unimplemented_dxgi_format():
Expand All @@ -137,8 +131,9 @@ def test_uncompressed_rgb():
assert im.mode == "RGBA"
assert im.size == (800, 600)

with Image.open(TEST_FILE_UNCOMPRESSED_RGB.replace(".dds", ".png")) as target:
assert_image_equal(target, im)
assert_image_equal_tofile(
im, TEST_FILE_UNCOMPRESSED_RGB.replace(".dds", ".png")
)


def test__validate_true():
Expand Down
12 changes: 9 additions & 3 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

from PIL import EpsImagePlugin, Image, features

from .helper import assert_image_similar, hopper, skip_unless_feature
from .helper import (
assert_image_similar,
assert_image_similar_tofile,
hopper,
skip_unless_feature,
)

HAS_GHOSTSCRIPT = EpsImagePlugin.has_ghostscript()

Expand Down Expand Up @@ -72,8 +77,9 @@ def test_cmyk():
assert cmyk_image.mode == "RGB"

if features.check("jpg"):
with Image.open("Tests/images/pil_sample_rgb.jpg") as target:
assert_image_similar(cmyk_image, target, 10)
assert_image_similar_tofile(
cmyk_image, "Tests/images/pil_sample_rgb.jpg", 10
)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
Expand Down
5 changes: 2 additions & 3 deletions Tests/test_file_fli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from PIL import FliImagePlugin, Image

from .helper import assert_image_equal, is_pypy
from .helper import assert_image_equal_tofile, is_pypy

# created as an export of a palette image from Gimp2.6
# save as...-> hopper.fli, default options.
Expand Down Expand Up @@ -122,5 +122,4 @@ def test_seek():
with Image.open(animated_test_file) as im:
im.seek(50)

with Image.open("Tests/images/a_fli.png") as expected:
assert_image_equal(im, expected)
assert_image_equal_tofile(im, "Tests/images/a_fli.png")
5 changes: 2 additions & 3 deletions Tests/test_file_ftex.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from PIL import Image

from .helper import assert_image_equal, assert_image_similar
from .helper import assert_image_equal_tofile, assert_image_similar


def test_load_raw():
with Image.open("Tests/images/ftex_uncompressed.ftu") as im:
with Image.open("Tests/images/ftex_uncompressed.png") as target:
assert_image_equal(im, target)
assert_image_equal_tofile(im, "Tests/images/ftex_uncompressed.png")


def test_load_dxt1():
Expand Down
8 changes: 3 additions & 5 deletions Tests/test_file_gbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from PIL import GbrImagePlugin, Image

from .helper import assert_image_equal
from .helper import assert_image_equal_tofile


def test_invalid_file():
Expand All @@ -14,13 +14,11 @@ def test_invalid_file():

def test_gbr_file():
with Image.open("Tests/images/gbr.gbr") as im:
with Image.open("Tests/images/gbr.png") as target:
assert_image_equal(target, im)
assert_image_equal_tofile(im, "Tests/images/gbr.png")


def test_multiple_load_operations():
with Image.open("Tests/images/gbr.gbr") as im:
im.load()
im.load()
with Image.open("Tests/images/gbr.png") as target:
assert_image_equal(target, im)
assert_image_equal_tofile(im, "Tests/images/gbr.png")
7 changes: 3 additions & 4 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .helper import (
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
hopper,
is_pypy,
Expand Down Expand Up @@ -317,8 +318,7 @@ def test_dispose_none_load_end():
with Image.open("Tests/images/dispose_none_load_end.gif") as img:
img.seek(1)

with Image.open("Tests/images/dispose_none_load_end_second.gif") as expected:
assert_image_equal(img, expected)
assert_image_equal_tofile(img, "Tests/images/dispose_none_load_end_second.gif")


def test_dispose_background():
Expand Down Expand Up @@ -629,8 +629,7 @@ def test_comment_over_255(tmp_path):

def test_zero_comment_subblocks():
with Image.open("Tests/images/hopper_zero_comment_subblocks.gif") as im:
with Image.open(TEST_GIF) as expected:
assert_image_equal(im, expected)
assert_image_equal_tofile(im, TEST_GIF)


def test_version(tmp_path):
Expand Down
5 changes: 2 additions & 3 deletions Tests/test_file_icns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from PIL import IcnsImagePlugin, Image, features

from .helper import assert_image_equal, assert_image_similar
from .helper import assert_image_equal, assert_image_similar_tofile

# sample icon file
TEST_FILE = "Tests/images/pillow.icns"
Expand Down Expand Up @@ -49,8 +49,7 @@ def test_save_append_images(tmp_path):
with Image.open(TEST_FILE) as im:
im.save(temp_file, append_images=[provided_im])

with Image.open(temp_file) as reread:
assert_image_similar(reread, im, 1)
assert_image_similar_tofile(im, temp_file, 1)

with Image.open(temp_file) as reread:
reread.size = (16, 16, 2)
Expand Down
5 changes: 2 additions & 3 deletions Tests/test_file_ico.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from PIL import IcoImagePlugin, Image, ImageDraw

from .helper import assert_image_equal, hopper
from .helper import assert_image_equal, assert_image_equal_tofile, hopper

TEST_ICO_FILE = "Tests/images/hopper.ico"

Expand Down Expand Up @@ -120,5 +120,4 @@ def test_draw_reloaded(tmp_path):

with Image.open(outfile) as im:
im.save("Tests/images/hopper_draw.ico")
with Image.open("Tests/images/hopper_draw.ico") as reloaded:
assert_image_equal(im, reloaded)
assert_image_equal_tofile(im, "Tests/images/hopper_draw.ico")
5 changes: 2 additions & 3 deletions Tests/test_file_im.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from PIL import Image, ImImagePlugin

from .helper import assert_image_equal, hopper, is_pypy
from .helper import assert_image_equal_tofile, hopper, is_pypy

# sample im
TEST_IM = "Tests/images/hopper.im"
Expand Down Expand Up @@ -86,8 +86,7 @@ def roundtrip(mode):
out = str(tmp_path / "temp.im")
im = hopper(mode)
im.save(out)
with Image.open(out) as reread:
assert_image_equal(reread, im)
assert_image_equal_tofile(im, out)

for mode in ["RGB", "P", "PA"]:
roundtrip(mode)
Expand Down
9 changes: 5 additions & 4 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from .helper import (
assert_image,
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
assert_image_similar_tofile,
cjpeg_available,
djpeg_available,
hopper,
Expand Down Expand Up @@ -577,15 +579,15 @@ def test_save_low_quality_baseline_qtables(self):
def test_load_djpeg(self):
with Image.open(TEST_FILE) as img:
img.load_djpeg()
assert_image_similar(img, Image.open(TEST_FILE), 5)
assert_image_similar_tofile(img, TEST_FILE, 5)

@pytest.mark.skipif(not cjpeg_available(), reason="cjpeg not available")
def test_save_cjpeg(self, tmp_path):
with Image.open(TEST_FILE) as img:
tempfile = str(tmp_path / "temp.jpg")
JpegImagePlugin._save_cjpeg(img, 0, tempfile)
# Default save quality is 75%, so a tiny bit of difference is alright
assert_image_similar(img, Image.open(tempfile), 17)
assert_image_similar_tofile(img, tempfile, 17)

def test_no_duplicate_0x1001_tag(self):
# Arrange
Expand Down Expand Up @@ -767,8 +769,7 @@ def test_photoshop(self):

# Test that the image can still load, even with broken Photoshop data
# This image had the APP13 length hexedited to be smaller
with Image.open("Tests/images/photoshop-200dpi-broken.jpg") as im_broken:
assert_image_equal(im_broken, im)
assert_image_equal_tofile(im, "Tests/images/photoshop-200dpi-broken.jpg")

# This image does not contain a Photoshop header string
with Image.open("Tests/images/app13.jpg") as im:
Expand Down
Loading

0 comments on commit 058b8d3

Please sign in to comment.