/Doc/library/imghdr.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 71 lines · 52 code · 19 blank · 0 comment · 0 complexity · 5fbdd0b197af403aadd6f7fe65ddf135 MD5 · raw file

  1. :mod:`imghdr` --- Determine the type of an image
  2. ================================================
  3. .. module:: imghdr
  4. :synopsis: Determine the type of image contained in a file or byte stream.
  5. The :mod:`imghdr` module determines the type of image contained in a file or
  6. byte stream.
  7. The :mod:`imghdr` module defines the following function:
  8. .. function:: what(filename[, h])
  9. Tests the image data contained in the file named by *filename*, and returns a
  10. string describing the image type. If optional *h* is provided, the *filename*
  11. is ignored and *h* is assumed to contain the byte stream to test.
  12. The following image types are recognized, as listed below with the return value
  13. from :func:`what`:
  14. +------------+-----------------------------------+
  15. | Value | Image format |
  16. +============+===================================+
  17. | ``'rgb'`` | SGI ImgLib Files |
  18. +------------+-----------------------------------+
  19. | ``'gif'`` | GIF 87a and 89a Files |
  20. +------------+-----------------------------------+
  21. | ``'pbm'`` | Portable Bitmap Files |
  22. +------------+-----------------------------------+
  23. | ``'pgm'`` | Portable Graymap Files |
  24. +------------+-----------------------------------+
  25. | ``'ppm'`` | Portable Pixmap Files |
  26. +------------+-----------------------------------+
  27. | ``'tiff'`` | TIFF Files |
  28. +------------+-----------------------------------+
  29. | ``'rast'`` | Sun Raster Files |
  30. +------------+-----------------------------------+
  31. | ``'xbm'`` | X Bitmap Files |
  32. +------------+-----------------------------------+
  33. | ``'jpeg'`` | JPEG data in JFIF or Exif formats |
  34. +------------+-----------------------------------+
  35. | ``'bmp'`` | BMP files |
  36. +------------+-----------------------------------+
  37. | ``'png'`` | Portable Network Graphics |
  38. +------------+-----------------------------------+
  39. .. versionadded:: 2.5
  40. Exif detection.
  41. You can extend the list of file types :mod:`imghdr` can recognize by appending
  42. to this variable:
  43. .. data:: tests
  44. A list of functions performing the individual tests. Each function takes two
  45. arguments: the byte-stream and an open file-like object. When :func:`what` is
  46. called with a byte-stream, the file-like object will be ``None``.
  47. The test function should return a string describing the image type if the test
  48. succeeded, or ``None`` if it failed.
  49. Example::
  50. >>> import imghdr
  51. >>> imghdr.what('/tmp/bass.gif')
  52. 'gif'