How to check if a file is a valid image file?

2025-03-13 08:54:00
admin
原创
41
摘要:问题描述:I am currently using PIL.from PIL import Image try: im = Image.open(filename) # do stuff except IOError: # filename not an image file How...

问题描述:

I am currently using PIL.

from PIL import Image

try:
    im = Image.open(filename)
    # do stuff
except IOError:
    # filename not an image file

However, while this sufficiently covers most cases, some image files like, xcf, svg and psd are not being detected. PSD files throw an OverflowError exception.

Is there someway I could include them as well?


解决方案 1:

您可以使用内置的imghdr模块。摘自其文档:

imghdr 模块确定文件或字节流中包含的图像类型。

使用方法如下:

>>> import imghdr
>>> imghdr.what('/tmp/bass')
'gif'

注意:从 Python 3.11 开始, imghdr 已被弃用,因为它仅支持少数文件格式。

解决方案 2:

除了 Brian 的建议之外,您还可以使用 PIL 的验证方法来检查文件是否损坏。

im.验证()

尝试确定文件是否损坏,而不实际解码图像数据。如果此方法发现任何问题,它会引发适当的异常。此方法仅适用于新打开的图像;如果图像已加载,则结果未定义。此外,如果您需要在使用此方法后加载图像,则必须重新打开图像文件。属性

解决方案 3:

Additionally to the PIL image check you can also add file name extension check like this:

filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif'))

Note that this only checks if the file name has a valid image extension, it does not actually open the image to see if it's a valid image, that's why you need to use additionally PIL or one of the libraries suggested in the other answers.

解决方案 4:

One option is to use the filetype package.

Installation

python -m pip install filetype

Advantages

  1. Fast: Does its work by loading only the first few bytes of your image (check on the magic number)

  2. Supports different mime type: Images, Videos, Fonts, Audio, Archives.

Example

filetype >= 1.0.7

import filetype

filename = "/path/to/file.jpg"

if filetype.is_image(filename):
    print(f"{filename} is a valid image...")
elif filetype.is_video(filename):
    print(f"{filename} is a valid video...")

filetype <= 1.0.6

import filetype

filename = "/path/to/file.jpg"

if filetype.image(filename):
    print(f"{filename} is a valid image...")
elif filetype.video(filename):
    print(f"{filename} is a valid video...")

Additional information on the official repo: https://github.com/h2non/filetype.py

解决方案 5:

很多时候,前几个字符对于各种文件格式来说都是一个神奇的数字。除了上面的异常检查之外,您还可以检查这一点。

解决方案 6:

更新

我还在GitHub 上的Python 脚本中实现了以下解决方案。

我还验证了损坏的文件 (jpg) 通常不是“损坏”的图像,即损坏的图片文件有时仍然是合法的图片文件,原始图像丢失或更改,但您仍然可以加载它而不会出现错误。但是,文件截断总是会导致错误。

结束更新

您可以使用 Python Pillow (PIL) 模块(适用于大多数图像格式)来检查文件是否是有效且完整的图像文件。

如果您想要检测损坏的图像,@Nadia Alramli 正确地建议了该im.verify()方法,但这并不能检测到所有可能的图像缺陷,例如,im.verify不能检测到截断的图像(大多数查看器通常会加载灰色区域)。

Pillow也能够检测到这些类型的缺陷,但您必须应用图像处理或图像解码/重新编码才能触发检查。最后我建议使用此代码:

from PIL import Image

try:
  im = Image.load(filename)
  im.verify() #I perform also verify, don't know if he sees other types o defects
  im.close() #reload is necessary in my case
  im = Image.load(filename) 
  im.transpose(Image.FLIP_LEFT_RIGHT)
  im.close()
except: 
  #manage excetions here

如果出现图像缺陷,此代码将引发异常。请考虑 im.verify 比执行图像处理快约 100 倍(我认为翻转是成本较低的转换之一)。使用此代码,您将以大约 10 MBytes/sec 的速度使用标准 Pillow 或以 40 MBytes/sec 的速度使用 Pillow-SIMD 模块(现代 2.5Ghz x86_64 CPU)验证一组图像。

For the other formats xcf,.. you can use Imagemagick wrapper Wand, the code is as follows:
Check the Wand documentation: here, to installation: here

im = wand.image.Image(filename=filename)
temp = im.flip;
im.close()

But, from my experiments Wand does not detect truncated images, I think it loads lacking parts as greyed area without prompting.

I red that Imagemagick has an external command identify that could make the job, but I have not found a way to invoke that function programmatically and I have not tested this route.

I suggest to always perform a preliminary check, check the filesize to not be zero (or very small), is a very cheap idea:

import os

statfile = os.stat(filename)
filesize = statfile.st_size
if filesize == 0:
  #manage here the 'faulty image' case

解决方案 7:

On Linux, you could use python-magic which uses libmagic to identify file formats.

AFAIK, libmagic looks into the file and tries to tell you more about it than just the format, like bitmap dimensions, format version etc.. So you might see this as a superficial test for "validity".

For other definitions of "valid" you might have to write your own tests.

解决方案 8:

You could use the Python bindings to libmagic, python-magic and then check the mime types. This won't tell you if the files are corrupted or intact but it should be able to determine what type of image it is.

解决方案 9:

Adapting from Fabiano and Tiago's answer.

from PIL import Image

def check_img(filename):
    try:
        im = Image.open(filename)
        im.verify()
        im.close()
        im = Image.open(filename) 
        im.transpose(Image.FLIP_LEFT_RIGHT)
        im.close()
        return True
    except: 
        print(filename,'corrupted')
        return False

if not check_img('/dir/image'):
    print('do something')

解决方案 10:

format = [".jpg",".png",".jpeg"]
 for (path,dirs,files) in os.walk(path):
     for file in files:
         if file.endswith(tuple(format)):
             print(path)
             print ("Valid",file)
         else:
             print(path)
             print("InValid",file)

解决方案 11:

Extension of the image can be used to check image file as follows.

import os
for f in os.listdir(folderPath):
    if (".jpg" in f) or (".bmp" in f):
        filePath = os.path.join(folderPath, f)
 
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1969  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1440  
  在企业运营过程中,跨部门协作效率的高低直接影响着项目的推进速度与质量,进而关乎企业的整体竞争力。PLM(产品生命周期管理)项目管理软件作为一种强大的工具,正逐渐在提升跨部门协作效率方面发挥着关键作用。它打破了部门之间的信息壁垒,优化了业务流程,为企业各部门之间的协同工作提供了有力支持。PLM项目管理软件打破信息壁垒信息...
plm合规性管理   24  
  PLM(Product Lifecycle Management)软件,即产品生命周期管理软件,旨在助力企业对产品从概念设计到退役处理的全生命周期进行有效管理。通过整合产品数据、流程以及人员,PLM软件能显著提升企业的创新能力、生产效率并降低成本。然而,PLM软件的实施并非易事,众多企业在落地过程中遭遇诸多挑战。要实现...
国内plm系统排名   31  
  研发过程的可视化对于企业提升效率、保障项目顺利推进至关重要。PLM(产品生命周期管理)系统作为整合产品全生命周期信息的重要工具,与甘特图相结合,能为研发过程可视化提供强大支持。通过对PLM系统支撑下甘特图的优化展现,可以让项目团队成员、管理层等清晰了解研发进度、资源分配等关键信息,从而做出更科学的决策。接下来,我们将详...
plm系统主要干什么的   22  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用