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