Decode example image file into image data. Args: value (`str` or `dict`): A string with the absolute image file path, a dictionary with keys: - `path`: String with absolute or relative image file path. - `bytes`: T
(self, value: dict, token_per_repo_id=None)
| 137 | ) |
| 138 | |
| 139 | def decode_example(self, value: dict, token_per_repo_id=None) -> "PIL.Image.Image": |
| 140 | """Decode example image file into image data. |
| 141 | |
| 142 | Args: |
| 143 | value (`str` or `dict`): |
| 144 | A string with the absolute image file path, a dictionary with |
| 145 | keys: |
| 146 | |
| 147 | - `path`: String with absolute or relative image file path. |
| 148 | - `bytes`: The bytes of the image file. |
| 149 | token_per_repo_id (`dict`, *optional*): |
| 150 | To access and decode |
| 151 | image files from private repositories on the Hub, you can pass |
| 152 | a dictionary repo_id (`str`) -> token (`bool` or `str`). |
| 153 | |
| 154 | Returns: |
| 155 | `PIL.Image.Image` |
| 156 | """ |
| 157 | if not self.decode: |
| 158 | raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead.") |
| 159 | |
| 160 | if config.PIL_AVAILABLE: |
| 161 | import PIL.Image |
| 162 | import PIL.ImageOps |
| 163 | else: |
| 164 | raise ImportError("To support decoding images, please install 'Pillow'.") |
| 165 | |
| 166 | if token_per_repo_id is None: |
| 167 | token_per_repo_id = {} |
| 168 | |
| 169 | path, bytes_ = value["path"], value["bytes"] |
| 170 | if bytes_ is None: |
| 171 | if path is None: |
| 172 | raise ValueError(f"An image should have one of 'path' or 'bytes' but both are None in {value}.") |
| 173 | else: |
| 174 | if is_local_path(path): |
| 175 | image = PIL.Image.open(path) |
| 176 | else: |
| 177 | source_url = path.split("::")[-1] |
| 178 | pattern = ( |
| 179 | config.HUB_DATASETS_URL |
| 180 | if source_url.startswith(config.HF_ENDPOINT) |
| 181 | else config.HUB_DATASETS_HFFS_URL |
| 182 | ) |
| 183 | source_url_fields = string_to_dict(source_url, pattern) |
| 184 | token = ( |
| 185 | token_per_repo_id.get(source_url_fields["repo_id"]) if source_url_fields is not None else None |
| 186 | ) |
| 187 | download_config = DownloadConfig(token=token) |
| 188 | with xopen(path, "rb", download_config=download_config) as f: |
| 189 | bytes_ = BytesIO(f.read()) |
| 190 | image = PIL.Image.open(bytes_) |
| 191 | else: |
| 192 | image = PIL.Image.open(BytesIO(bytes_)) |
| 193 | image.load() # to avoid "Too many open files" errors |
| 194 | if image.getexif().get(PIL.Image.ExifTags.Base.Orientation) is not None: |
| 195 | image = PIL.ImageOps.exif_transpose(image) |
| 196 | if self.mode and self.mode != image.mode: |