Is there a way to determine in Python (or another language) to see if the JPG image is corrupted?

I was wondering if there is a way to detect a JPEG file in Python (or another language) and determine if it is corrupted (for example, if I finish downloading a JPG file before it finishes, then I cannot open the file and view it)? Are there libraries that make this easy?

+5
source share
3 answers

You can try using PIL. But simply opening a truncated JPG file will not fail, and none of the methods will verify. Attempting to load it will throw an exception, however;

First we use a nice jpg file:

> du mvc-002f.jpg
56  mvc-002f.jpg
> dd if=mvc-002f.jpg of=broken.jpg bs=1k count=20
20+0 records in
20+0 records out
20480 bytes transferred in 0.000133 secs (154217856 bytes/sec)

Then we try the Python image library:

>>> import Image
>>> im = Image.open('broken.jpg')
>>> im.verify()
>>> im = Image.open('broken.jpg')  # im.verify() invalidates the file pointer
>>> im.load()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/PIL/ImageFile.py", line 201, in load
    raise IOError("image file is truncated (%d bytes not processed)" % len(b))
IOError: image file is truncated (16 bytes not processed)

user827992, .

+6

PIL:

import Image

def is_image_ok(fn):
    try:
        Image.open(fn)
        return True
    except:
        return False
0

.

JPEG , .

"" , , , undefined, , , JPEG , , - , JPEG, , , , , , .

Also, the file header may be corrupted, but in this case, your file is probably designated as corrupted, not caring about "what is", corrupted, as any general file may be.

0
source

All Articles