def get_image_source(self, image): if image is None: return elif uri_is_external(image.uri): return image.uri else: image.stream.seek(0) data = image.stream.read() _, filename = posixpath.split(image.uri) extension = filename.split('.')[-1].lower() b64_encoded_src = 'data:image/{ext};base64,{data}'.format( ext=extension, data=base64.b64encode(data).decode(), ) return self.escape(b64_encoded_src)
def parse_image(self, el, parsed, stack): x, y = self._get_image_size(el) relationship_id = self._get_image_id(el) try: image_part = self.current_part.get_part_by_id( relationship_id=relationship_id, ) is_uri_external = uri_is_external(image_part.uri) if is_uri_external: data = image_part.uri else: data = image_part.stream.read() except KeyError: return '' _, filename = posixpath.split(image_part.uri) return self.image( data, filename, x, y, uri_is_external=is_uri_external, )