Skip to content

Commit

Permalink
Allows truncated images in PIL.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684820302
  • Loading branch information
marcenacp authored and The TensorFlow Datasets Authors committed Oct 11, 2024
1 parent 960a2df commit 565eb47
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tensorflow_datasets/core/utils/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from __future__ import annotations

import csv
import functools
import subprocess
from typing import Any, List, Optional

Expand All @@ -34,6 +35,21 @@
THUMBNAIL_SIZE = 128


@functools.cache
def _allow_pil_to_load_truncated_images():
"""Allows truncated images.
The function is cached to be called only once. Activating this flag solves
issues where the image cannot be converted to RGB.
"""
try:
from PIL import ImageFile # pylint: disable=g-import-not-at-top

ImageFile.LOAD_TRUNCATED_IMAGES = True
except ImportError:
pass


@py_utils.memoize()
def _get_runner():
return tf_utils.TFGraphRunner()
Expand Down Expand Up @@ -148,6 +164,7 @@ def _postprocess_noop(img: PilImage) -> PilImage:


def _postprocess_convert_rgb(img: PilImage) -> PilImage:
_allow_pil_to_load_truncated_images()
return img.convert('RGB')


Expand Down

0 comments on commit 565eb47

Please sign in to comment.