Home >Backend Development >Python Tutorial >Python method to determine image integrity based on pillow

Python method to determine image integrity based on pillow

高洛峰
高洛峰Original
2017-01-14 13:19:321818browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn