Skip to content

Commit 9c4e001

Browse files
author
The TensorFlow Datasets Authors
committed
Fix Croissant dataset builder to use PNG encoding for images.
PiperOrigin-RevId: 652771520
1 parent c6210d0 commit 9c4e001

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

tensorflow_datasets/core/features/image_feature.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class _ImageEncoder:
9494

9595
def __post_init__(self):
9696
self.np_dtype = dtype_utils.cast_to_numpy(self.dtype)
97+
# When encoding isn't defined, default to PNG.
98+
if self.encoding_format is None:
99+
self.encoding_format = 'png'
97100

98101
# TODO(tfds): Should deprecate the TFGraph runner in favor of simpler
99102
# implementation
@@ -123,15 +126,12 @@ def _encode_image(self, np_image: np.ndarray) -> bytes:
123126
"""Returns np_image encoded as jpeg or png."""
124127
_validate_np_array(np_image, shape=self.shape, dtype=self.np_dtype)
125128

126-
# When encoding isn't defined, default to PNG.
127129
# Should we be more strict about explicitly define the encoding (raise
128130
# error / warning instead) ?
129131
# It has created subtle issues for imagenet_corrupted: images are read as
130132
# JPEG images to apply some processing, but final image saved as PNG
131133
# (default) rather than JPEG.
132-
return self._runner.run(
133-
_ENCODE_FN[self.encoding_format or 'png'](), np_image
134-
)
134+
return self._runner.run(_ENCODE_FN[self.encoding_format](), np_image)
135135

136136
def _encode_pil_image(self, pil_image) -> bytes:
137137
"""Encode a PIL Image object to bytes.
@@ -144,7 +144,7 @@ def _encode_pil_image(self, pil_image) -> bytes:
144144
"""
145145
check_pil_import_or_raise_error()
146146
buffer = io.BytesIO()
147-
pil_image.save(buffer, format=self.encoding_format or pil_image.format)
147+
pil_image.save(buffer, format=self.encoding_format)
148148
return buffer.getvalue()
149149

150150
def decode_image(self, img: tf.Tensor) -> tf.Tensor:

0 commit comments

Comments
 (0)