My goal is to compute the average brightness of an image displayed at the given url.
import requests
from PIL import ImageStat, UnidentifiedImageError
from PIL import Image as ImPIL
def brightness(imgUrl):
response = requests.get(imgUrl)
try:
img = ImPIL.open(response.raw)
img = img.convert('L')
stat = ImageStat.Stat(img)
return stat.mean[0] # Average brightness of pixels in given image
except UnidentifiedImageError:
print(imgUrl)
The images I am using are paintings hosted on Wikidata/Wikimedia. Here are some of the URLs that trigger the exception:
http://commons.wikimedia.org/wiki/Special:FilePath/15-10-27-Els%20Quatre%20Gats-RalfR-WMA%202740a.jpg
http://commons.wikimedia.org/wiki/Special:FilePath/Garrote%20vil%2C%20de%20Ram%C3%B3n%20Casas.jpg
http://commons.wikimedia.org/wiki/Special:FilePath/La%20morfina%20%28Santiago%20Rusi%C3%B1ol%29.jpg
http://commons.wikimedia.org/wiki/Special:FilePath/Ramon%20Casas%20-%20Over%20My%20Dead%20Body%20-%20Google%20Art%20Project.jpg
What am I missing ?