Skip to content

Commit

Permalink
Merge pull request #4436 from hugovk/pytest.importorskip
Browse files Browse the repository at this point in the history
Use pytest.importorskip to skip on a missing import dependency
  • Loading branch information
radarhere committed Feb 20, 2020
2 parents 30f6177 + f43efb0 commit f87505c
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 393 deletions.
11 changes: 2 additions & 9 deletions Tests/test_file_fpx.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import pytest
from PIL import Image

try:
from PIL import FpxImagePlugin
except ImportError:
olefile_installed = False
else:
olefile_installed = True

pytestmark = pytest.mark.skipif(
not olefile_installed, reason="olefile package not installed"
FpxImagePlugin = pytest.importorskip(
"PIL.FpxImagePlugin", reason="olefile not installed"
)


Expand Down
17 changes: 4 additions & 13 deletions Tests/test_file_mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@

from .helper import assert_image_similar, hopper, skip_unless_feature

try:
from PIL import MicImagePlugin
except ImportError:
olefile_installed = False
else:
olefile_installed = True

MicImagePlugin = pytest.importorskip(
"PIL.MicImagePlugin", reason="olefile not installed"
)
pytestmark = skip_unless_feature("libtiff")
TEST_FILE = "Tests/images/hopper.mic"


pytestmark = [
pytest.mark.skipif(not olefile_installed, reason="olefile package not installed"),
skip_unless_feature("libtiff"),
]


def test_sanity():
with Image.open(TEST_FILE) as im:
im.load()
Expand Down
180 changes: 88 additions & 92 deletions Tests/test_file_webp_alpha.py
Original file line number Diff line number Diff line change
@@ -1,119 +1,115 @@
import unittest

import pytest
from PIL import Image

from .helper import PillowTestCase, assert_image_equal, assert_image_similar, hopper
from .helper import assert_image_equal, assert_image_similar, hopper

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


def setup_module():
if _webp.WebPDecoderBuggyAlpha():
pytest.skip("Buggy early version of WebP installed, not testing transparency")

try:
from PIL import _webp
except ImportError:
_webp = None

def test_read_rgba():
"""
Can we read an RGBA mode file without error?
Does it have the bits we expect?
"""

@unittest.skipIf(_webp is None, "WebP support not installed")
class TestFileWebpAlpha(PillowTestCase):
def setUp(self):
if _webp.WebPDecoderBuggyAlpha(self):
self.skipTest(
"Buggy early version of WebP installed, not testing transparency"
)
# Generated with `cwebp transparent.png -o transparent.webp`
file_path = "Tests/images/transparent.webp"
with Image.open(file_path) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "WEBP"
image.load()
image.getdata()

def test_read_rgba(self):
"""
Can we read an RGBA mode file without error?
Does it have the bits we expect?
"""
image.tobytes()

# Generated with `cwebp transparent.png -o transparent.webp`
file_path = "Tests/images/transparent.webp"
with Image.open(file_path) as image:
self.assertEqual(image.mode, "RGBA")
self.assertEqual(image.size, (200, 150))
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
with Image.open("Tests/images/transparent.png") as target:
assert_image_similar(image, target, 20.0)

image.tobytes()

with Image.open("Tests/images/transparent.png") as target:
assert_image_similar(image, target, 20.0)
def test_write_lossless_rgb(tmp_path):
"""
Can we write an RGBA mode file with lossless compression without error?
Does it have the bits we expect?
"""

def test_write_lossless_rgb(self):
"""
Can we write an RGBA mode file with lossless compression without
error? Does it have the bits we expect?
"""
temp_file = str(tmp_path / "temp.webp")
# temp_file = "temp.webp"

temp_file = self.tempfile("temp.webp")
# temp_file = "temp.webp"
pil_image = hopper("RGBA")

pil_image = hopper("RGBA")
mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
# Add some partially transparent bits:
pil_image.paste(mask, (0, 0), mask)

mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128))
# Add some partially transparent bits:
pil_image.paste(mask, (0, 0), mask)
pil_image.save(temp_file, lossless=True)

pil_image.save(temp_file, lossless=True)
with Image.open(temp_file) as image:
image.load()

with Image.open(temp_file) as image:
image.load()
assert image.mode == "RGBA"
assert image.size == pil_image.size
assert image.format == "WEBP"
image.load()
image.getdata()

self.assertEqual(image.mode, "RGBA")
self.assertEqual(image.size, pil_image.size)
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
assert_image_equal(image, pil_image)

assert_image_equal(image, pil_image)

def test_write_rgba(self):
"""
Can we write a RGBA mode file to webp without error.
Does it have the bits we expect?
"""
def test_write_rgba(tmp_path):
"""
Can we write a RGBA mode file to WebP without error.
Does it have the bits we expect?
"""

temp_file = self.tempfile("temp.webp")
temp_file = str(tmp_path / "temp.webp")

pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
pil_image.save(temp_file)
pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20))
pil_image.save(temp_file)

if _webp.WebPDecoderBuggyAlpha(self):
return
if _webp.WebPDecoderBuggyAlpha():
return

with Image.open(temp_file) as image:
image.load()
with Image.open(temp_file) as image:
image.load()

self.assertEqual(image.mode, "RGBA")
self.assertEqual(image.size, (10, 10))
self.assertEqual(image.format, "WEBP")
image.load()
image.getdata()
assert image.mode == "RGBA"
assert image.size == (10, 10)
assert image.format == "WEBP"
image.load()
image.getdata()

# early versions of webp are known to produce higher deviations:
# deal with it
if _webp.WebPDecoderVersion(self) <= 0x201:
assert_image_similar(image, pil_image, 3.0)
else:
assert_image_similar(image, pil_image, 1.0)
# Early versions of WebP are known to produce higher deviations:
# deal with it
if _webp.WebPDecoderVersion() <= 0x201:
assert_image_similar(image, pil_image, 3.0)
else:
assert_image_similar(image, pil_image, 1.0)

def test_write_unsupported_mode_PA(self):
"""
Saving a palette-based file with transparency to WebP format
should work, and be similar to the original file.
"""

temp_file = self.tempfile("temp.webp")
file_path = "Tests/images/transparent.gif"
def test_write_unsupported_mode_PA(tmp_path):
"""
Saving a palette-based file with transparency to WebP format
should work, and be similar to the original file.
"""

temp_file = str(tmp_path / "temp.webp")
file_path = "Tests/images/transparent.gif"
with Image.open(file_path) as im:
im.save(temp_file)
with Image.open(temp_file) as image:
assert image.mode == "RGBA"
assert image.size == (200, 150)
assert image.format == "WEBP"

image.load()
image.getdata()
with Image.open(file_path) as im:
im.save(temp_file)
with Image.open(temp_file) as image:
self.assertEqual(image.mode, "RGBA")
self.assertEqual(image.size, (200, 150))
self.assertEqual(image.format, "WEBP")

image.load()
image.getdata()
with Image.open(file_path) as im:
target = im.convert("RGBA")

assert_image_similar(image, target, 25.0)
target = im.convert("RGBA")

assert_image_similar(image, target, 25.0)

0 comments on commit f87505c

Please sign in to comment.