Write a segmentation mask and label mapping, as PNG format. ### Parameters: - `path: Union[str, os.PathLike, IO]` The file path or file object to write to. - `mask: np.ndarray` The segmentation mask, uint8 or uint16 array of shape (H, W). - `labels: Dict[str, int
(path: Union[str, os.PathLike, IO], mask: np.ndarray, labels: Dict[str, int] = None, compression_level: int = 7)
| 177 | |
| 178 | |
| 179 | def write_segmentation(path: Union[str, os.PathLike, IO], mask: np.ndarray, labels: Dict[str, int] = None, compression_level: int = 7): |
| 180 | """ |
| 181 | Write a segmentation mask and label mapping, as PNG format. |
| 182 | ### Parameters: |
| 183 | - `path: Union[str, os.PathLike, IO]` |
| 184 | The file path or file object to write to. |
| 185 | - `mask: np.ndarray` |
| 186 | The segmentation mask, uint8 or uint16 array of shape (H, W). |
| 187 | - `labels: Dict[str, int] = None` |
| 188 | The label mapping, a dictionary of {label_name: label_id}. |
| 189 | - `compression_level: int = 7` |
| 190 | The compression level for PNG compression. |
| 191 | """ |
| 192 | assert mask.dtype == np.uint8 or mask.dtype == np.uint16, f"Unsupported dtype {mask.dtype}" |
| 193 | pil_image = Image.fromarray(mask) |
| 194 | pnginfo = PngImagePlugin.PngInfo() |
| 195 | if labels is not None: |
| 196 | labels_json = json.dumps(labels, ensure_ascii=True, separators=(',', ':')) |
| 197 | pnginfo.add_text('labels', labels_json) |
| 198 | pil_image.save(path, pnginfo=pnginfo, compress_level=compression_level) |
| 199 | |
| 200 | |
| 201 |
nothing calls this directly
no outgoing calls
no test coverage detected