Skip to content

Commit

Permalink
Merge pull request #3169 from radarhere/support
Browse files Browse the repository at this point in the history
Add warnings if image file identification fails due to lack of support
  • Loading branch information
hugovk authored Sep 30, 2018
2 parents 748ec87 + a0fda60 commit 8344aec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
22 changes: 20 additions & 2 deletions Tests/test_file_webp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper

from PIL import Image
from PIL import Image, WebPImagePlugin

try:
from PIL import _webp
Expand All @@ -13,15 +13,27 @@ class TestFileWebp(PillowTestCase):

def setUp(self):
if not HAVE_WEBP:
self.skipTest('WebP support not installed')
return

self.rgb_mode = "RGB"

def test_unsupported(self):
if HAVE_WEBP:
WebPImagePlugin.SUPPORTED = False

file_path = "Tests/images/hopper.webp"
self.assert_warning(UserWarning,
lambda: self.assertRaises(IOError, Image.open, file_path))

if HAVE_WEBP:
WebPImagePlugin.SUPPORTED = True

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_version(self):
_webp.WebPDecoderVersion()
_webp.WebPDecoderBuggyAlpha()

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_read_rgb(self):
"""
Can we read a RGB mode WebP file without error?
Expand All @@ -41,6 +53,7 @@ def test_read_rgb(self):
self.assert_image_similar_tofile(
image, 'Tests/images/hopper_webp_bits.ppm', 1.0)

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_write_rgb(self):
"""
Can we write a RGB mode file to webp without error.
Expand Down Expand Up @@ -70,6 +83,7 @@ def test_write_rgb(self):
target = hopper(self.rgb_mode)
self.assert_image_similar(image, target, 12.0)

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_write_unsupported_mode_L(self):
"""
Saving a black-and-white file to WebP format should work, and be
Expand All @@ -90,6 +104,7 @@ def test_write_unsupported_mode_L(self):

self.assert_image_similar(image, target, 10.0)

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_write_unsupported_mode_P(self):
"""
Saving a palette-based file to WebP format should work, and be
Expand All @@ -110,6 +125,7 @@ def test_write_unsupported_mode_P(self):

self.assert_image_similar(image, target, 50.0)

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_WebPEncode_with_invalid_args(self):
"""
Calling encoder functions with no arguments should result in an error.
Expand All @@ -119,6 +135,7 @@ def test_WebPEncode_with_invalid_args(self):
self.assertRaises(TypeError, _webp.WebPAnimEncoder)
self.assertRaises(TypeError, _webp.WebPEncode)

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_WebPDecode_with_invalid_args(self):
"""
Calling decoder functions with no arguments should result in an error.
Expand All @@ -128,6 +145,7 @@ def test_WebPDecode_with_invalid_args(self):
self.assertRaises(TypeError, _webp.WebPAnimDecoder)
self.assertRaises(TypeError, _webp.WebPDecode)

@unittest.skipIf(not HAVE_WEBP, "WebP support not installed")
def test_no_resource_warning(self):
file_path = "Tests/images/hopper.webp"
image = Image.open(file_path)
Expand Down
8 changes: 7 additions & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2615,11 +2615,15 @@ def open(fp, mode="r"):

preinit()

accept_warnings = []
def _open_core(fp, filename, prefix):
for i in ID:
try:
factory, accept = OPEN[i]
if not accept or accept(prefix):
result = not accept or accept(prefix)
if type(result) in [str, bytes]:
accept_warnings.append(result)
elif result:
fp.seek(0)
im = factory(fp, filename)
_decompression_bomb_check(im.size)
Expand All @@ -2643,6 +2647,8 @@ def _open_core(fp, filename, prefix):

if exclusive_fp:
fp.close()
for message in accept_warnings:
warnings.warn(message)
raise IOError("cannot identify image file %r"
% (filename if filename else fp))

Expand Down
23 changes: 16 additions & 7 deletions src/PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from . import Image, ImageFile, _webp
from . import Image, ImageFile
try:
from . import _webp
SUPPORTED = True
except ImportError as e:
SUPPORTED = False
from io import BytesIO


Expand All @@ -25,7 +30,10 @@ def _accept(prefix):
is_webp_file = prefix[8:12] == b"WEBP"
is_valid_vp8_mode = prefix[12:16] in _VP8_MODES_BY_IDENTIFIER

return is_riff_file_format and is_webp_file and is_valid_vp8_mode
if is_riff_file_format and is_webp_file and is_valid_vp8_mode:
if not SUPPORTED:
return "image file could not be identified because WEBP support not installed"
return True


class WebPImageFile(ImageFile.ImageFile):
Expand Down Expand Up @@ -321,8 +329,9 @@ def _save(im, fp, filename):


Image.register_open(WebPImageFile.format, WebPImageFile, _accept)
Image.register_save(WebPImageFile.format, _save)
if _webp.HAVE_WEBPANIM:
Image.register_save_all(WebPImageFile.format, _save_all)
Image.register_extension(WebPImageFile.format, ".webp")
Image.register_mime(WebPImageFile.format, "image/webp")
if SUPPORTED:
Image.register_save(WebPImageFile.format, _save)
if _webp.HAVE_WEBPANIM:
Image.register_save_all(WebPImageFile.format, _save_all)
Image.register_extension(WebPImageFile.format, ".webp")
Image.register_mime(WebPImageFile.format, "image/webp")

0 comments on commit 8344aec

Please sign in to comment.