MCPcopy Create free account
hub / github.com/apple/ml-sharp / write_image

Function write_image

src/sharp/utils/io.py:127–149  ·  view source on GitHub ↗

Write image to binary stream.

(
    image: np.ndarray,
    output_io: IO[bytes],
    format="jpg",
    icc_profile: list[bytes] | None = None,
    jpeg_quality: int = 92,
)

Source from the content-addressed store, hash-verified

125
126
127def 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
152def get_supported_image_extensions(with_heic: bool = True) -> list[str]:

Callers 1

save_imageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected