Show More
@@ -97,7 +97,9 b' def date_default(obj):' | |||
|
97 | 97 | |
|
98 | 98 | # constants for identifying png/jpeg data |
|
99 | 99 | PNG = b'\x89PNG\r\n\x1a\n' |
|
100 | PNG64 = encodebytes(PNG) | |
|
100 | 101 | JPEG = b'\xff\xd8' |
|
102 | JPEG64 = encodebytes(JPEG) | |
|
101 | 103 | |
|
102 | 104 | def encode_images(format_dict): |
|
103 | 105 | """b64-encodes images in a displaypub format dict |
@@ -120,12 +122,21 b' def encode_images(format_dict):' | |||
|
120 | 122 | |
|
121 | 123 | """ |
|
122 | 124 | encoded = format_dict.copy() |
|
125 | ||
|
123 | 126 | pngdata = format_dict.get('image/png') |
|
124 |
if isinstance(pngdata, bytes) |
|
|
125 | encoded['image/png'] = encodebytes(pngdata).decode('ascii') | |
|
127 | if isinstance(pngdata, bytes): | |
|
128 | # make sure we don't double-encode | |
|
129 | if pngdata[:13] != PNG64: | |
|
130 | pngdata = encodebytes(pngdata) | |
|
131 | encoded['image/png'] = pngdata.decode('ascii') | |
|
132 | ||
|
126 | 133 | jpegdata = format_dict.get('image/jpeg') |
|
127 |
if isinstance(jpegdata, bytes) |
|
|
128 | encoded['image/jpeg'] = encodebytes(jpegdata).decode('ascii') | |
|
134 | if isinstance(jpegdata, bytes): | |
|
135 | # make sure we don't double-encode | |
|
136 | if jpegdata[:5] != JPEG64: | |
|
137 | jpegdata = encodebytes(jpegdata) | |
|
138 | encoded['image/jpeg'] = jpegdata.decode('ascii') | |
|
139 | ||
|
129 | 140 | return encoded |
|
130 | 141 | |
|
131 | 142 |
General Comments 0
You need to be logged in to leave comments.
Login now