PYTHON   17

image.py

Guest on 3rd June 2021 05:46:30 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.