English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Method for Checking Image Integrity Based on Pillow in Python

This article describes a method for determining the integrity of images based on pillow in Python. Shared for everyone's reference, as follows:

1Install third-party libraries.

pip install pillow

2Function examples.

# encoding=utf-8
# author: walker
# date: 2016-07-26
# summary: Determine the validity of the image
import io
from PIL import Image
# Determine if the file is a valid (complete) image
# 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
# Input parameter is bytes, such as binary data returned by network requests
def IsValidImage4Bytes(buf):
  bValid = True
  try:
    Image.open(io.BytesIO(buf)).verify()
  except:
    bValid = False
  return bValid

Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python Image Operation Techniques', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Techniques', 'Summary of Python Function Usage Techniques', 'Summary of Python String Operation Techniques', 'Classic Tutorial of Python Entry and Progression', and 'Summary of Python File and Directory Operation Techniques'.

I hope the content described in this article will be helpful to everyone's Python program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like