From 768965a328ac28daff348ad5f030b09954db555c Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Sun, 21 Jun 2020 11:21:16 +0300 Subject: [PATCH] Handle UnidentifiedImageError gracefully --- nm_iconfinder/nm_iconfinder.py | 7 +++++-- setup.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/nm_iconfinder/nm_iconfinder.py b/nm_iconfinder/nm_iconfinder.py index 25011cb..b4c8dbe 100644 --- a/nm_iconfinder/nm_iconfinder.py +++ b/nm_iconfinder/nm_iconfinder.py @@ -5,7 +5,7 @@ from io import BytesIO from bs4 import BeautifulSoup -from PIL import Image +from PIL import Image, UnidentifiedImageError import requests TIMEOUT = 3 # seconds @@ -40,7 +40,10 @@ def from_url(cls, url: str) -> Optional["Icon"]: return None with BytesIO(res.content) as bio: - img = Image.open(bio) + try: + img = Image.open(bio) + except UnidentifiedImageError: + return None width, height = img.size # Ignore non-square Icons if width != height: diff --git a/setup.py b/setup.py index 821567a..4619682 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='nm_iconfinder', packages=['nm_iconfinder'], - version='0.1', + version='0.1.1', license='ISC', description='Find icons of a website given a URL', author='newsmail.today',