โมดูล imghdr ในไลบรารีมาตรฐานของ Python กำหนดประเภทภาพที่อยู่ในไฟล์หรือไบต์สตรีม มีเพียงฟังก์ชันเดียวที่กำหนดในโมดูล imghdr
imghdr.what(filename, h=None):
ฟังก์ชันนี้ทดสอบข้อมูลภาพที่อยู่ในไฟล์และคืนค่าสตริงที่อธิบายประเภทภาพ ฟังก์ชันยังยอมรับพารามิเตอร์ h หากให้มา filename จะถูกละเว้นและ h จะถูกจัดการเป็นไบต์สตรีมที่จะทดสอบ
โมดูล imghdr รู้จักประเภทภาพต่อไปนี้:
| 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 |
| 'webp' | WebP files |
| 'exr' | OpenEXR Files |
ตัวอย่าง
>> import imghdr
>>> imghdr.what('bass.gif')
'gif'
>>> imghdr.what('polar.png')
'png'
>>> imghdr.what('test.jpg')
'jpeg'