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

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

imghdr模块提供的函数如下:

 imghdr.what(filename, h=None)
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.
 imghdr.tests
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. 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:

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

 #python imghdr

 #Author   :   Hongten
#Mailto : hongtenzone@foxmail.com
#Blog : http://www.cnblogs.com/hongten
#QQ : 648719819
#Version : 1.0
#Create : 2013-09-09 import os
import imghdr #global var
#image path
IMG_PATH = None __author__ = ['Hongten'] def get_ext(path):
'''return a string describing the image type.'''
if os.path.exists(path):
return imghdr.what(path)
else:
print('the path [{}] dose not exit!'.format(path)) def init():
global IMG_PATH
IMG_PATH = 'C:\\test\\hongten.jpg' def main():
init()
img_ext = get_ext(IMG_PATH)
print('the image : [{}],and the type is :[{}]'.format(IMG_PATH, img_ext)) if __name__ == '__main__':
main()

运行效果:

the image : [C:\test\hongten.jpg],and the type is :[jpeg]

更多参考:

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

python开发_imghdr_图像格式支持的更多相关文章

  1. (转)Python开发程序:支持多用户在线的FTP程序

    原文链接:http://www.itnose.net/detail/6642756.html 作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ...

  2. Ubuntu14.04安装pycharm用于Python开发环境部署,并且支持pycharm使用中文输入

    一.目标 实现在Linux下用pycharm调试工具/Python开发 Linux使用vi/vim工具写Python,缺点:调试不方便,无代码提示.跳转等诸多功能. Windows使用idle/pyc ...

  3. 更好地使用Atom支持基于Jupyter的Python开发

    有关于使用Atom进行Python开发的网上资料比较少,最近发现使用Atom结合Hydrogen插件进行Python开发,尤其是数据挖掘相关的工作,整体体验要好于Vscode,Vscode虽然说也有连 ...

  4. python开发_常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctype ...

  5. Python 开发轻量级爬虫05

    Python 开发轻量级爬虫 (imooc总结05--网页下载器) 介绍网页下载器 网页下载器是将互联网上url对应的网页下载到本地的工具.因为将网页下载到本地才能进行后续的分析处理,可以说网页下载器 ...

  6. Python开发【前端】:JavaScript

    JavaScript入门 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...

  7. Python开发【前端】:HTML

    HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏 ...

  8. 在Ubuntu下配置舒服的Python开发环境

    Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的一个安装和配置步骤,基于 Ubuntu 12.0 ...

  9. 转[开发环境配置]在Ubuntu下配置舒服的Python开发环境

    在Ubuntu下配置舒服的Python开发环境 Ubuntu 提供了一个良好的 Python 开发环境,但如果想使我们的开发效率最大化,还需要进行很多定制化的安装和配置.下面的是我们团队开发人员推荐的 ...

随机推荐

  1. 「要买车网」免费获取汽车电商要买车网购车优惠券 - 持续更新(2016-03-12)www.fortunelab.cn

    汽车电商要买车网简介 “要买车”(www.yaomaiche.com)网站是上海运图投资有限公司旗下网站,是首家真正打通交易闭环的汽车电商网站,由中国电子商务成功探索者——卜广齐于2014年10月在上 ...

  2. Django集成Xadmin list index out of range报错解决方案

    return self.render(context) File "C:\Python36\lib\site-packages\django\template\defaulttags.py& ...

  3. MySQL 修改数据

    UPDATE 语句 修改或更新 MySQL 中的数据,我们可以使用 SQL UPDATE 命令来操作. 可以同时修改 一个 或 多个 字段: 可以在where子句中指定条件: 可以在一个单独表中更新数 ...

  4. 将ipa文件安装到测试设备上的几种方法

    Installing Your App on Test Devices Using Xcode You can install iOS App files on devices using Xcode ...

  5. linux快速安装mysql教程

    #安装mysql服务器:yum install mysql-server #设置开机启动chkconfig mysqld on#现在启动服务service mysqld start #设置root初始 ...

  6. C# TimeSpan获取 年月

    public static string GetYearMonthDayString(this DateTime expires) { try { var now = DateTime.Now; Ti ...

  7. css文字环绕图片--遇到的问题及解决方法

    一.前言 需要实现一个文字环绕图片的效果,心想so easy嘛. 1)代码部分 <style> .img-left { border: 3px solid #005588; width:3 ...

  8. mvn简单命令

    导出maven项目依赖的jar包 mvn dependency:copy-dependencies -DoutputDirectory=lib 编译Java代码 mvn compile eclipse ...

  9. C++ 必须使用初始化列表

    继承关系中,父类无默认构造函数 类类型类成员变量无默认构造函数 const类型成员变量 引用类型成员变量 不使用初始化列表,在创建对象调用构造函数之前会对所有的成员变量进行默认初始化,然后再执行构造函 ...

  10. SQLSERVER中的非工作时间不得插入数据的触发器的实现

    create trigger trigger_nameon table_namefor insert,update,deleteasif (datepart(yy,getdate())%4=0 or ...