ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](sndhdr.xhtml "sndhdr --- 推测声音文件的类型") | - [上一页](colorsys.xhtml "colorsys --- Conversions between color systems") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [多媒体服务](mm.xhtml) » - $('.inline-search').show(0); | # [`imghdr`](#module-imghdr "imghdr: Determine the type of image contained in a file or byte stream.") --- 推测图像类型 **源代码** [Lib/imghdr.py](https://github.com/python/cpython/tree/3.7/Lib/imghdr.py) \[https://github.com/python/cpython/tree/3.7/Lib/imghdr.py\] - - - - - - [`imghdr`](#module-imghdr "imghdr: Determine the type of image contained in a file or byte stream.") 模块推测文件或字节流中的图像的类型。 [`imghdr`](#module-imghdr "imghdr: Determine the type of image contained in a file or byte stream.") 模块定义了以下类型: `imghdr.``what`(*filename*, *h=None*)测试包含在命名为 *filename* 的文件中的图像数据,并且返回描述此类图片的字符串。如果可选的 *h* 被提供, *filename* 将被忽略并且 *h* 包含将被测试的二进制流。 在 3.6 版更改: 接受一个 [类路径对象](../glossary.xhtml#term-path-like-object)。 接下来的图像类型是可识别的,返回值来自 [`what()`](#imghdr.what "imghdr.what"): 值 图像格式 `'rgb'` SGI 图像库文件 `'gif'` GIF 87a 和 89a 文件 `'pbm'` 便携式位图文件 `'pgm'` 便携式灰度图文件 `'ppm'` 便携式像素表文件 `'tiff'` TIFF 文件 `'rast'` Sun 光栅文件 `'xbm'` X 位图文件 `'jpeg'` JFIF 或 Exif 格式的 JPEG 数据 `'bmp'` BMP 文件 `'png'` 便携式网络图像 `'webp'` WebP 文件 `'exr'` OpenEXR 文件 3\.5 新版功能: *exr* 和 *webp* 格式被添加。 你可以扩展此 [`imghdr`](#module-imghdr "imghdr: Determine the type of image contained in a file or byte stream.") 可以被追加的这个变量识别的文件格式的列表: `imghdr.``tests`执行单个测试的函数列表。每个函数都有两个参数:字节流和类似开放文件的对象。当 [`what()`](#imghdr.what "imghdr.what") 用字节流调用时,类文件对象将是 `None`。 如果测试陈工,这个测试函数应当返回一个描述图像类型的字符串,否则返回 `None`。 示例: ``` >>> import imghdr >>> imghdr.what('bass.gif') 'gif' ``` ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](sndhdr.xhtml "sndhdr --- 推测声音文件的类型") | - [上一页](colorsys.xhtml "colorsys --- Conversions between color systems") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python 标准库](index.xhtml) » - [多媒体服务](mm.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。