def make_image(data: Optional[bytes]) -> io.BytesIO: image_buffer = None if data: image_buffer = io.BytesIO(data) try: Image.from_blob(image_buffer.getbuffer()) except UnrecognizedImageError: image_buffer = None if not image_buffer: broken_img_path = pathlib.Path(__file__).parent / "image-broken.png" image_buffer = io.BytesIO(broken_img_path.read_bytes()) return image_buffer
def make_image(data): image_buffer = None if data: image_buffer = io.BytesIO(data) try: Image.from_blob(image_buffer.getbuffer()) except (UnrecognizedImageError, UnexpectedEndOfFileError, InvalidImageStreamError): image_buffer = None if not image_buffer: broken_img = load_inline_image(IMG_BLANK) image_buffer = io.BytesIO(broken_img) return image_buffer
def it_can_construct_from_an_image_blob(self, blob_, BytesIO_, _from_stream_, stream_, image_): image = Image.from_blob(blob_) BytesIO_.assert_called_once_with(blob_) _from_stream_.assert_called_once_with(stream_, blob_) assert image is image_
def image_size( image_buffer: io.BytesIO, width_px: Optional[int] = None, height_px: Optional[int] = None, ) -> Dict[str, int]: """ Compute width and height to feed python-docx so that image is contained in the page and respects width_px and height_px. Return: Empty: No resize Single dimension (width or height): image ratio is expected to be maintained Two dimensions (width and height): image should be resized to dimensions """ image = Image.from_blob(image_buffer.getbuffer()) # Normalize image size to inches. # - Without a specified pixel size, images are their actual pixel size, so that # images of the same pixel size appear the same size in the document, regardless # of their resolution. # - With a specified pixel size, images should take the specified size, regardless # of their resolution. if height_px is None: height = image.px_height / image.vert_dpi else: height = height_px / DEFAULT_DPI if width_px is None: width = image.px_width / image.horz_dpi else: width = width_px / DEFAULT_DPI height = Inches(height) width = Inches(width) size = {} if width > USABLE_WIDTH: new_height = round(image.px_height / (image.px_width / USABLE_WIDTH)) if new_height > USABLE_HEIGHT: size["height"] = USABLE_HEIGHT else: size["width"] = USABLE_WIDTH elif height > USABLE_HEIGHT: new_width = round(image.px_width / (image.px_height / USABLE_HEIGHT)) if new_width > USABLE_WIDTH: size["width"] = USABLE_WIDTH else: size["height"] = USABLE_HEIGHT else: if width_px is not None: size["width"] = width if height_px is not None: size["height"] = height return size
def image_size( image_buffer: io.BytesIO, width_px: Optional[int] = None, height_px: Optional[int] = None, ) -> Dict[str, int]: """ Compute width and height to feed python-docx so that image is contained in the page and respects width_px and height_px. Return: Empty: No resize Single dimension (width or height): image ratio is expected to be maintained Two dimensions (width and height): image should be resized to dimensions """ image = Image.from_blob(image_buffer.getbuffer()) height = image.px_height if height_px is None else height_px width = image.px_width if width_px is None else width_px height = Inches(height / image.vert_dpi) width = Inches(width / image.horz_dpi) size = {} if width > USABLE_WIDTH: new_height = round(image.px_height / (image.px_width / USABLE_WIDTH)) if new_height > USABLE_HEIGHT: size["height"] = USABLE_HEIGHT else: size["width"] = USABLE_WIDTH elif height > USABLE_HEIGHT: new_width = round(image.px_width / (image.px_height / USABLE_HEIGHT)) if new_width > USABLE_WIDTH: size["width"] = USABLE_WIDTH else: size["height"] = USABLE_HEIGHT else: if width_px is not None: size["width"] = width if height_px is not None: size["height"] = height return size
def it_can_construct_from_an_image_blob(self, from_blob_fixture): blob_, BytesIO_, _from_stream_, stream_, image_ = from_blob_fixture image = Image.from_blob(blob_) BytesIO_.assert_called_once_with(blob_) _from_stream_.assert_called_once_with(stream_, blob_) assert image is image_
def image(self): if self._image is None: self._image = Image.from_blob(self.blob) return self._image