Home >Backend Development >Python Tutorial >Python method to determine image integrity based on pillow
The example in this article describes Python's method of judging the integrity of images based on pillow. Share it with everyone for your reference, the details are as follows:
1. Install third-party libraries.
pip install pillow
2. Function example.
#encoding=utf-8 #author: walker #date: 2016-07-26 #summary: 判断图片的有效性 import io from PIL import Image #判断文件是否为有效(完整)的图片 #输入参数为文件路径 def IsValidImage(pathfile): bValid = True try: Image.open(pathfile).verify() except: bValid = False return bValid #判断文件是否为有效(完整)的图片 #输入参数为bytes,如网络请求返回的二进制数据 def IsValidImage4Bytes(buf): bValid = True try: Image.open(io.BytesIO(buf)).verify() except: bValid = False return bValid
I hope this article will be helpful to everyone in Python programming.
For more articles related to Python’s pillow-based method of judging image integrity, please pay attention to the PHP Chinese website!