mime types
[software/python-on-guile.git] / modules / language / python / module / email / mime / image.py
1 module(email,mime,image)
2
3 # Copyright (C) 2001-2006 Python Software Foundation
4 # Author: Barry Warsaw
5 # Contact: email-sig@python.org
6
7 """Class representing image/* type MIME documents."""
8
9 __all__ = ['MIMEImage']
10
11 import imghdr
12
13 import email.encoders as encoders
14 from email.mime.nonmultipart import MIMENonMultipart
15
16
17
18 class MIMEImage(MIMENonMultipart):
19 """Class for generating image/* type MIME documents."""
20
21 def __init__(self, _imagedata, _subtype=None,
22 _encoder=encoders.encode_base64, *, policy=None, **_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, policy=policy,
47 **_params)
48 self.set_payload(_imagedata)
49 _encoder(self)