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