Commit 696b0d80 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add truncated check function

parent 413457f0
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -1213,3 +1213,4 @@ fabric.properties
/extra
/resource/yolort/*
!/resource/yolort/Makefile
!/test/testfile/**/*
 No newline at end of file
+17 −1
Original line number Diff line number Diff line
@@ -18,4 +18,20 @@
[![Contributors](https://img.shields.io/github/contributors/deepghs/imgutils)](https://github.com/deepghs/imgutils/graphs/contributors)
[![GitHub license](https://img.shields.io/github/license/deepghs/imgutils)](https://github.com/deepghs/imgutils/blob/master/LICENSE)

Utilties for Images
Utilities for Images


## How to check truncated image

```python
from imgutils.validate import is_truncated_file

if __name__ == '__main__':
    filename = 'test_jpg.jpg'
    if is_truncated_file(filename):
        print('This image is truncated, you\'d better '
              'remove this shit from your dataset.')
    else:
        print('This image is okay!')

```
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
from .truncate import is_truncated_file
+27 −0
Original line number Diff line number Diff line
from contextlib import contextmanager
from threading import Lock

from PIL import Image, ImageFile

_LOCK = Lock()


@contextmanager
def _mock_load_truncated_images(value: bool):
    with Lock():
        _load = ImageFile.LOAD_TRUNCATED_IMAGES
        ImageFile.LOAD_TRUNCATED_IMAGES = value
        try:
            yield
        finally:
            ImageFile.LOAD_TRUNCATED_IMAGES = _load


@_mock_load_truncated_images(False)
def is_truncated_file(path: str) -> bool:
    try:
        Image.open(path).load()
    except OSError:
        return True
    else:
        return False
+1.81 MiB
Loading image diff...
Loading