Python's method for judging image integrity based on pillow

  • 2020-05-10 18:25:09
  • OfStack

This article illustrates an example of how Python judges image integrity based on pillow. I will share it with you for your reference as follows:

1. Install the third party library.


pip install pillow

2. Function examples.


#encoding=utf-8
#author: walker
#date: 2016-07-26
#summary:  Judge the validity of the image 
import io
from PIL import Image
# Determine if the file is a valid (complete) image 
# The input parameter is the file path 
def IsValidImage(pathfile):
  bValid = True
  try:
    Image.open(pathfile).verify()
  except:
    bValid = False
  return bValid
# Determine if the file is a valid (complete) image 
# The input parameter is bytes , as the network request returned 2 Hexadecimal data 
def IsValidImage4Bytes(buf):
  bValid = True
  try:
    Image.open(io.BytesIO(buf)).verify()
  except:
    bValid = False
  return bValid

More about Python related content interested readers to view this site project: Python pictures skills summary, "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article has helped you with your Python programming.


Related articles: