首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

wxpython知道文件的路径,如何确定是否是图片

2013-07-08 
wxpython知道文件的路径,怎么确定是否是图片如题。。dialog wx.lib.imagebrowser.ImageDialog(self, None)

wxpython知道文件的路径,怎么确定是否是图片
如题。。


dialog = wx.lib.imagebrowser.ImageDialog(self, None)
if(dialog.ShowModal() == wx.ID_OK):
     self.directory = dialog.GetFile()

由self.directory怎么知道这个文件是不是图片。。我不想要逐个图片格式的测试。。。 wxPython Python
[解决办法]
python有imghdr模块做这个事,见http://docs.python.org/2/library/imghdr.html。

引自上面的文档
The imghdr module determines the type of image contained in a file or byte stream.

The imghdr module defines the following function:

imghdr.what(filename[, h])

    Tests the image data contained in the file named by filename, and returns a string describing the image type. If optional h is provided, the filename is ignored and h is assumed to contain the byte stream to test.

The following image types are recognized, as listed below with the return value from what():
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

热点排行