There are a couple of things that needs to be done to verify if a file from web is an image or not.
PIL.Image
method accepts a file object or a string for local file, whereas you are trying to provide a web link. [You can download the file, save it locally and then open it.]
- You need to catch the exception, if there is any, and then display if the file is bad or not.
I have added the basic code below.
from PIL import Image
import requests
from PIL import UnidentifiedImageError
url = 'https://furniload.com/furni/js_c16_lounger.png'
response = requests.get(url)
# download and save fiel from web
with open("sample_image.png", "wb") as file_object:
file_object.write(response.content)
# pass the local file to PIL
local_image = "sample_image.png"
try:
img = Image.open("sample_image.png")
img.verify()
# if file is not an image, catch error and display message
except UnidentifiedImageError:
print('Bad file:' + local_image)
OUTPUT :
Bad file:sample_image.png