Home | History | Annotate | Download | only in mime
      1 # Copyright (C) 2001-2006 Python Software Foundation
      2 # Author: Barry Warsaw
      3 # Contact: email-sig (at] python.org
      4 
      5 """Class representing image/* type MIME documents."""
      6 
      7 __all__ = ['MIMEImage']
      8 
      9 import imghdr
     10 
     11 from email import encoders
     12 from email.mime.nonmultipart import MIMENonMultipart
     13 
     14 
     15 
     17 class MIMEImage(MIMENonMultipart):
     18     """Class for generating image/* type MIME documents."""
     19 
     20     def __init__(self, _imagedata, _subtype=None,
     21                  _encoder=encoders.encode_base64, **_params):
     22         """Create an image/* type MIME document.
     23 
     24         _imagedata is a string containing the raw image data.  If this data
     25         can be decoded by the standard Python `imghdr' module, then the
     26         subtype will be automatically included in the Content-Type header.
     27         Otherwise, you can specify the specific image subtype via the _subtype
     28         parameter.
     29 
     30         _encoder is a function which will perform the actual encoding for
     31         transport of the image data.  It takes one argument, which is this
     32         Image instance.  It should use get_payload() and set_payload() to
     33         change the payload to the encoded form.  It should also add any
     34         Content-Transfer-Encoding or other headers to the message as
     35         necessary.  The default encoding is Base64.
     36 
     37         Any additional keyword arguments are passed to the base class
     38         constructor, which turns them into parameters on the Content-Type
     39         header.
     40         """
     41         if _subtype is None:
     42             _subtype = imghdr.what(None, _imagedata)
     43         if _subtype is None:
     44             raise TypeError('Could not guess image MIME subtype')
     45         MIMENonMultipart.__init__(self, 'image', _subtype, **_params)
     46         self.set_payload(_imagedata)
     47         _encoder(self)
     48