Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dectect images filetype
02-07-2010, 01:57 PM (This post was last modified: 02-07-2010 02:15 PM by codecaine.)
Post: #1
Dectect images filetype
This is a example of detecting images filetypes without need extensions. Here is a list of file types it can detect.

PHP Code:
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 
Code:
import imghdr;

filetype = imghdr.what('blah'); #dectect image type blah is the image file in the current directory with no file extension
if filetype == None:
    print 'Cannot determine file type';
    sys.exit(1);
print('The file type is a ' + filetype); #print image type
Visit this user's website Find all posts by this user
Quote this message in a reply
02-07-2010, 07:40 PM
Post: #2
RE: Dectect images filetype
This is so fast. I can see it is in "imghdr" library. Very good.

We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up.
Bram Cohen - Bit Torrent Creator
Visit this user's website Find all posts by this user
Quote this message in a reply
02-07-2010, 08:39 PM
Post: #3
RE: Dectect images filetype
Thanks for sharing.
Is imghdr included in standard Python libs?
Find all posts by this user
Quote this message in a reply
02-07-2010, 09:01 PM
Post: #4
RE: Dectect images filetype
It is standard in Python 2.2 definitely because this code works for me !

We attack any new problem we encounter with techniques we already know, and try small modifications if difficulties turn up.
Bram Cohen - Bit Torrent Creator
Visit this user's website Find all posts by this user
Quote this message in a reply
02-07-2010, 09:02 PM
Post: #5
RE: Dectect images filetype
(02-07-2010 09:01 PM)drdebcol Wrote:  It is standard in Python 2.2 definitely because this code works for me !

kk thanks bro.
gonna try it myself. Smile
Find all posts by this user
Quote this message in a reply
02-08-2010, 12:51 AM (This post was last modified: 02-08-2010 01:04 AM by codecaine.)
Post: #6
RE: Dectect images filetype
(02-07-2010 08:39 PM)Alphablend Wrote:  Thanks for sharing.
Is imghdr included in standard Python libs?

It is a standard library in python.
yes is is a standard module when you install python it comes with a great help file on windows its in the start menu -> python it goes over all the modules etc in python
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: