Write image to binary stream.
(
image: np.ndarray,
output_io: IO[bytes],
format="jpg",
icc_profile: list[bytes] | None = None,
jpeg_quality: int = 92,
)
| 125 | |
| 126 | |
| 127 | def write_image( |
| 128 | image: np.ndarray, |
| 129 | output_io: IO[bytes], |
| 130 | format="jpg", |
| 131 | icc_profile: list[bytes] | None = None, |
| 132 | jpeg_quality: int = 92, |
| 133 | ): |
| 134 | """Write image to binary stream.""" |
| 135 | pil_config = {} |
| 136 | if format == "JPEG": |
| 137 | pil_config["quality"] = jpeg_quality |
| 138 | |
| 139 | image_pil = Image.fromarray(image) |
| 140 | |
| 141 | # Workaround to error [io.UnsupportedOperation: seek]. |
| 142 | if format == "TIFF": |
| 143 | bytes_io = io.BytesIO() |
| 144 | image_pil.save(bytes_io, format="TIFF") |
| 145 | bytes_io.seek(0) |
| 146 | output_io.write(bytes_io.read()) |
| 147 | return |
| 148 | |
| 149 | image_pil.save(output_io, format, icc_profile=icc_profile, **pil_config) |
| 150 | |
| 151 | |
| 152 | def get_supported_image_extensions(with_heic: bool = True) -> list[str]: |