2007-08-29 22:15:14 -03:00
|
|
|
# Copyright (C) 2001-2006 Python Software Foundation
|
|
|
|
# Author: Barry Warsaw
|
|
|
|
# Contact: email-sig@python.org
|
|
|
|
|
|
|
|
"""Class representing image/* type MIME documents."""
|
|
|
|
|
|
|
|
__all__ = ['MIMEImage']
|
|
|
|
|
|
|
|
from email import encoders
|
|
|
|
from email.mime.nonmultipart import MIMENonMultipart
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
class MIMEImage(MIMENonMultipart):
|
|
|
|
"""Class for generating image/* type MIME documents."""
|
|
|
|
|
|
|
|
def __init__(self, _imagedata, _subtype=None,
|
|
|
|
_encoder=encoders.encode_base64, *, policy=None, **_params):
|
|
|
|
"""Create an image/* type MIME document.
|
|
|
|
|
2023-01-14 15:01:27 -04:00
|
|
|
_imagedata contains the bytes for the raw image data. If the data
|
2022-04-14 21:48:59 -03:00
|
|
|
type can be detected (jpeg, png, gif, tiff, rgb, pbm, pgm, ppm,
|
|
|
|
rast, xbm, bmp, webp, and exr attempted), then the subtype will be
|
|
|
|
automatically included in the Content-Type header. Otherwise, you can
|
|
|
|
specify the specific image subtype via the _subtype parameter.
|
|
|
|
|
|
|
|
_encoder is a function which will perform the actual encoding for
|
|
|
|
transport of the image data. It takes one argument, which is this
|
|
|
|
Image instance. It should use get_payload() and set_payload() to
|
|
|
|
change the payload to the encoded form. It should also add any
|
|
|
|
Content-Transfer-Encoding or other headers to the message as
|
|
|
|
necessary. The default encoding is Base64.
|
|
|
|
|
|
|
|
Any additional keyword arguments are passed to the base class
|
|
|
|
constructor, which turns them into parameters on the Content-Type
|
|
|
|
header.
|
|
|
|
"""
|
|
|
|
_subtype = _what(_imagedata) if _subtype is None else _subtype
|
|
|
|
if _subtype is None:
|
|
|
|
raise TypeError('Could not guess image MIME subtype')
|
|
|
|
MIMENonMultipart.__init__(self, 'image', _subtype, policy=policy,
|
|
|
|
**_params)
|
|
|
|
self.set_payload(_imagedata)
|
|
|
|
_encoder(self)
|
|
|
|
|
|
|
|
|
|
|
|
_rules = []
|
|
|
|
|
|
|
|
|
2022-04-13 14:47:41 -03:00
|
|
|
# Originally from the imghdr module.
|
2022-04-14 21:48:59 -03:00
|
|
|
def _what(data):
|
|
|
|
for rule in _rules:
|
|
|
|
if res := rule(data):
|
2022-04-13 14:47:41 -03:00
|
|
|
return res
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
def rule(rulefunc):
|
|
|
|
_rules.append(rulefunc)
|
|
|
|
return rulefunc
|
|
|
|
|
|
|
|
|
|
|
|
@rule
|
|
|
|
def _jpeg(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""JPEG data with JFIF or Exif markers; and raw JPEG"""
|
|
|
|
if h[6:10] in (b'JFIF', b'Exif'):
|
|
|
|
return 'jpeg'
|
|
|
|
elif h[:4] == b'\xff\xd8\xff\xdb':
|
|
|
|
return 'jpeg'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _png(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
if h.startswith(b'\211PNG\r\n\032\n'):
|
|
|
|
return 'png'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _gif(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""GIF ('87 and '89 variants)"""
|
|
|
|
if h[:6] in (b'GIF87a', b'GIF89a'):
|
|
|
|
return 'gif'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _tiff(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""TIFF (can be in Motorola or Intel byte order)"""
|
|
|
|
if h[:2] in (b'MM', b'II'):
|
|
|
|
return 'tiff'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _rgb(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""SGI image library"""
|
|
|
|
if h.startswith(b'\001\332'):
|
|
|
|
return 'rgb'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _pbm(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""PBM (portable bitmap)"""
|
|
|
|
if len(h) >= 3 and \
|
2022-04-14 21:48:59 -03:00
|
|
|
h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':
|
2022-04-13 14:47:41 -03:00
|
|
|
return 'pbm'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _pgm(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""PGM (portable graymap)"""
|
|
|
|
if len(h) >= 3 and \
|
2022-04-14 21:48:59 -03:00
|
|
|
h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
|
2022-04-13 14:47:41 -03:00
|
|
|
return 'pgm'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _ppm(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""PPM (portable pixmap)"""
|
|
|
|
if len(h) >= 3 and \
|
2022-04-14 21:48:59 -03:00
|
|
|
h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
|
2022-04-13 14:47:41 -03:00
|
|
|
return 'ppm'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _rast(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""Sun raster file"""
|
|
|
|
if h.startswith(b'\x59\xA6\x6A\x95'):
|
|
|
|
return 'rast'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _xbm(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
"""X bitmap (X10 or X11)"""
|
|
|
|
if h.startswith(b'#define '):
|
|
|
|
return 'xbm'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _bmp(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
if h.startswith(b'BM'):
|
|
|
|
return 'bmp'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _webp(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
if h.startswith(b'RIFF') and h[8:12] == b'WEBP':
|
|
|
|
return 'webp'
|
|
|
|
|
|
|
|
|
2022-04-14 21:48:59 -03:00
|
|
|
@rule
|
|
|
|
def _exr(h):
|
2022-04-13 14:47:41 -03:00
|
|
|
if h.startswith(b'\x76\x2f\x31\x01'):
|
|
|
|
return 'exr'
|