前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python开发_imghdr_图像格式支持

python开发_imghdr_图像格式支持

作者头像
Hongten
发布2018-09-13 12:09:18
5920
发布2018-09-13 12:09:18
举报
文章被收录于专栏:HongtenHongten

在python中,imghdr模块对图像格式提供了支持

该模块主要是处理识别图像的格式

imghdr模块提供的函数如下:

代码语言:javascript
复制
1 imghdr.what(filename, h=None) 
2        Tests the image data contained in the file named by filename, and returns a string describing the image type. If optional h is provided, t         he filename is ignored and h is assumed to contain the byte stream to test.
代码语言:javascript
复制
1 imghdr.tests 
2       A list of functions performing the individual tests. Each function takes two arguments: the byte-stream and an open file-like object. When         what() is called with a byte-stream, the file-like object will be None.
3 
4       The test function should return a string describing the image type if the test succeeded, or None if it failed.

以下的图像格式可以被识别:

Value

Image format

'rgb'

SGI ImgLib Files

'gif'

GIF 87a and 89a Files

'pbm'

Portable Bitmap Files

'pgm'

Portable Graymap Files

'ppm'

Portable Pixmap Files

'tiff'

TIFF Files

'rast'

Sun Raster Files

'xbm'

X Bitmap Files

'jpeg'

JPEG data in JFIF or Exif formats

'bmp'

BMP files

'png'

Portable Network Graphics

=====================================================

以下是我做的demo:

=====================================================

代码语言:javascript
复制
 1 #python imghdr
 2 
 3 #Author   :   Hongten
 4 #Mailto   :   hongtenzone@foxmail.com
 5 #Blog     :   http://www.cnblogs.com/hongten
 6 #QQ       :   648719819
 7 #Version  :   1.0
 8 #Create   :   2013-09-09
 9 
10 import os
11 import imghdr
12 
13 #global var
14 #image path
15 IMG_PATH = None
16 
17 __author__ = ['Hongten']
18 
19 def get_ext(path):
20     '''return a string describing the image type.'''
21     if os.path.exists(path):
22         return imghdr.what(path)
23     else:
24         print('the path [{}] dose not exit!'.format(path))
25 
26 def init():
27     global IMG_PATH
28     IMG_PATH = 'C:\\test\\hongten.jpg'
29 
30 def main():
31     init()
32     img_ext = get_ext(IMG_PATH)
33     print('the image : [{}],and the type is :[{}]'.format(IMG_PATH, img_ext))
34 
35 if __name__ == '__main__':
36     main()

运行效果:

代码语言:javascript
复制
the image : [C:\test\hongten.jpg],and the type is :[jpeg]

更多参考:

http://blog.sina.com.cn/s/blog_a04184c101010kt2.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档