Skip to content

Commit

Permalink
Replaced various instances of assert_image_similar with assert_image_…
Browse files Browse the repository at this point in the history
…similar_tofile
  • Loading branch information
radarhere committed Feb 21, 2021
1 parent a5c2510 commit 3495b31
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 142 deletions.
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_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: 3 additions & 2 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
assert_image_similar_tofile,
cjpeg_available,
djpeg_available,
hopper,
Expand Down Expand Up @@ -578,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
17 changes: 7 additions & 10 deletions Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .helper import (
assert_image_equal,
assert_image_similar,
assert_image_similar_tofile,
is_big_endian,
skip_unless_feature,
)
Expand Down Expand Up @@ -62,9 +63,7 @@ def test_invalid_file():
def test_bytesio():
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
data = BytesIO(f.read())
with Image.open(data) as im:
im.load()
assert_image_similar(im, test_card, 1.0e-3)
assert_image_similar_tofile(test_card, data, 1.0e-3)


# These two test pre-written JPEG 2000 files that were not written with
Expand All @@ -80,9 +79,9 @@ def test_lossless(tmp_path):


def test_lossy_tiled():
with Image.open("Tests/images/test-card-lossy-tiled.jp2") as im:
im.load()
assert_image_similar(im, test_card, 2.0)
assert_image_similar_tofile(
test_card, "Tests/images/test-card-lossy-tiled.jp2", 2.0
)


def test_lossless_rt():
Expand Down Expand Up @@ -193,15 +192,13 @@ def test_16bit_monochrome_has_correct_mode():
@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
def test_16bit_monochrome_jp2_like_tiff():
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
assert_image_similar(jp2, tiff_16bit, 1e-3)
assert_image_similar_tofile(tiff_16bit, "Tests/images/16bit.cropped.jp2", 1e-3)


@pytest.mark.xfail(is_big_endian(), reason="Fails on big-endian")
def test_16bit_monochrome_j2k_like_tiff():
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
assert_image_similar(j2k, tiff_16bit, 1e-3)
assert_image_similar_tofile(tiff_16bit, "Tests/images/16bit.cropped.j2k", 1e-3)


def test_16bit_j2k_roundtrips():
Expand Down
6 changes: 2 additions & 4 deletions Tests/test_file_libtiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ def test_compressions(self, tmp_path):

im.save(out, compression="jpeg", quality=30)
size_jpeg_30 = os.path.getsize(out)
with Image.open(out) as im3:
assert_image_similar(im2, im3, 30)
assert_image_similar_tofile(im2, out, 30)

assert size_raw > size_compressed
assert size_compressed > size_jpeg
Expand Down Expand Up @@ -642,8 +641,7 @@ def save_bytesio(compression=None):
pilim.save(buffer_io, format="tiff", compression=compression)
buffer_io.seek(0)

with Image.open(buffer_io) as pilim_load:
assert_image_similar(pilim, pilim_load, 0)
assert_image_similar_tofile(pilim, buffer_io, 0)

save_bytesio()
save_bytesio("raw")
Expand Down
10 changes: 7 additions & 3 deletions Tests/test_file_webp_alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

from PIL import Image

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

_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed")

Expand All @@ -29,8 +34,7 @@ def test_read_rgba():

image.tobytes()

with Image.open("Tests/images/transparent.png") as target:
assert_image_similar(image, target, 20.0)
assert_image_similar_tofile(image, "Tests/images/transparent.png", 20.0)


def test_write_lossless_rgb(tmp_path):
Expand Down
13 changes: 4 additions & 9 deletions Tests/test_file_wmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from PIL import Image, WmfImagePlugin

from .helper import assert_image_similar, hopper
from .helper import assert_image_similar_tofile, hopper


def test_load_raw():
Expand All @@ -13,19 +13,15 @@ def test_load_raw():
# Currently, support for WMF/EMF is Windows-only
im.load()
# Compare to reference rendering
with Image.open("Tests/images/drawing_emf_ref.png") as imref:
imref.load()
assert_image_similar(im, imref, 0)
assert_image_similar_tofile(im, "Tests/images/drawing_emf_ref.png", 0)

# Test basic WMF open and rendering
with Image.open("Tests/images/drawing.wmf") as im:
if hasattr(Image.core, "drawwmf"):
# Currently, support for WMF/EMF is Windows-only
im.load()
# Compare to reference rendering
with Image.open("Tests/images/drawing_wmf_ref.png") as imref:
imref.load()
assert_image_similar(im, imref, 2.0)
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref.png", 2.0)


def test_register_handler(tmp_path):
Expand Down Expand Up @@ -66,8 +62,7 @@ def test_load_set_dpi():
im.load(144)
assert im.size == (164, 164)

with Image.open("Tests/images/drawing_wmf_ref_144.png") as expected:
assert_image_similar(im, expected, 2.1)
assert_image_similar_tofile(im, "Tests/images/drawing_wmf_ref_144.png", 2.1)


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

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

Expand Down Expand Up @@ -61,8 +61,7 @@ def test_draw(request, tmp_path):
im = Image.new("L", (130, 30), "white")
draw = ImageDraw.Draw(im)
draw.text((0, 0), message, "black", font=font)
with Image.open("Tests/images/test_draw_pbm_target.png") as target:
assert_image_similar(im, target, 0)
assert_image_similar_tofile(im, "Tests/images/test_draw_pbm_target.png", 0)


def test_textsize(request, tmp_path):
Expand All @@ -83,8 +82,7 @@ def _test_high_characters(request, tmp_path, message):
im = Image.new("L", (750, 30), "white")
draw = ImageDraw.Draw(im)
draw.text((0, 0), message, "black", font=font)
with Image.open("Tests/images/high_ascii_chars.png") as target:
assert_image_similar(im, target, 0)
assert_image_similar_tofile(im, "Tests/images/high_ascii_chars.png", 0)


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

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

Expand Down Expand Up @@ -82,8 +82,7 @@ def _test_draw(request, tmp_path, encoding):
draw = ImageDraw.Draw(im)
message = charsets[encoding]["message"].encode(encoding)
draw.text((0, 0), message, "black", font=font)
with Image.open(charsets[encoding]["image1"]) as target:
assert_image_similar(im, target, 0)
assert_image_similar_tofile(im, charsets[encoding]["image1"], 0)


def test_draw_iso8859_1(request, tmp_path):
Expand Down
8 changes: 3 additions & 5 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .helper import (
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
assert_image_similar_tofile,
assert_not_all_same,
hopper,
is_win32,
Expand Down Expand Up @@ -172,8 +172,7 @@ def test_tempfile(self):
with tempfile.TemporaryFile() as fp:
im.save(fp, "JPEG")
fp.seek(0)
with Image.open(fp) as reloaded:
assert_image_similar(im, reloaded, 20)
assert_image_similar_tofile(im, fp, 20)

def test_unknown_extension(self, tmp_path):
im = hopper()
Expand Down Expand Up @@ -456,8 +455,7 @@ def test_effect_spread(self):

# Assert
assert im.size == (128, 128)
with Image.open("Tests/images/effect_spread.png") as im3:
assert_image_similar(im2, im3, 110)
assert_image_similar_tofile(im2, "Tests/images/effect_spread.png", 110)

def test_effect_spread_zero(self):
# Arrange
Expand Down
11 changes: 8 additions & 3 deletions Tests/test_imagecms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

from PIL import Image, ImageMode, features

from .helper import assert_image, assert_image_equal, assert_image_similar, hopper
from .helper import (
assert_image,
assert_image_equal,
assert_image_similar,
assert_image_similar_tofile,
hopper,
)

try:
from PIL import ImageCms
Expand Down Expand Up @@ -240,8 +246,7 @@ def test_lab_color():

# i.save('temp.lab.tif') # visually verified vs PS.

with Image.open("Tests/images/hopper.Lab.tif") as target:
assert_image_similar(i, target, 3.5)
assert_image_similar_tofile(i, "Tests/images/hopper.Lab.tif", 3.5)


def test_lab_srgb():
Expand Down
10 changes: 5 additions & 5 deletions Tests/test_imagedraw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .helper import (
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
assert_image_similar_tofile,
hopper,
skip_unless_feature,
)
Expand Down Expand Up @@ -62,7 +62,7 @@ def helper_ellipse(mode, bbox):
draw.ellipse(bbox, pen, brush)

# Assert
assert_image_similar(im, Image.open(expected), 1)
assert_image_similar_tofile(im, expected, 1)


def test_ellipse1():
Expand All @@ -83,7 +83,7 @@ def test_ellipse_edge():
draw.ellipse(((0, 0), (W - 1, H - 1)), brush)

# Assert
assert_image_similar(im, Image.open("Tests/images/imagedraw_ellipse_edge.png"), 1)
assert_image_similar_tofile(im, "Tests/images/imagedraw_ellipse_edge.png", 1)


def helper_line(points):
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_big_rectangle():
draw.rectangle(bbox, brush)

# Assert
assert_image_similar(im, Image.open(expected), 1)
assert_image_similar_tofile(im, expected, 1)


@skip_unless_feature("freetype2")
Expand All @@ -194,7 +194,7 @@ def test_text():
draw.text((5, 5), "ImageDraw2", font)

# Assert
assert_image_similar(im, Image.open(expected), 13)
assert_image_similar_tofile(im, expected, 13)


@skip_unless_feature("freetype2")
Expand Down
Loading

0 comments on commit 3495b31

Please sign in to comment.