Add simple svg tester for imghdr

This commit is contained in:
Takeshi KOMIYA 2017-06-25 18:51:49 +09:00
parent ea87cfba4f
commit 372e8f2c64

View File

@ -120,3 +120,17 @@ def parse_data_uri(uri):
image_data = base64.b64decode(data)
return DataURI(mimetype, charset, image_data)
def test_svg(h, f):
"""An additional imghdr library helper; test the header is SVG's or not."""
try:
if '<svg' in h.decode('utf-8').lower():
return 'svg+xml'
except UnicodeDecodeError:
pass
# install test_svg() to imghdr
# refs: https://docs.python.org/3.6/library/imghdr.html#imghdr.tests
imghdr.tests.append(test_svg)