I have scraped lots of images that were either jpg, jpeg, or png. However, some of my images do not show in thumbnail view because they are damaged. I have already increased the limit to 100MB for thumbnail view in Linux so now only the ones that are corrupted do not show as thumbnails.

How can I detect these images either using a Python code or bash script?

For example when I click on one of the PNGs, it looks like the following:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="3024px" height="3024px" viewBox="0 0 3024 3024" enable-background="new 0 0 3024 3024" xml:space="preserve">
<path d="M1515.778,205.669C981.54,205.669,476.37,135.396,0-0.008c0,1068.396,0,2756.137,0,3024.016
    C2547.501,136.957,2043.941,205.669,1515.778,205.669z M2353.338,463.804l-18.163,28.818

Some of the images though don't even open and are like this:

could not load image

text file icon

and when I open jpg images that don't have a thumbnail preview they are opened as text files with many weird characters. My ultimate goal is to automatically remove these damaged files rather than removing them manually by looking at their thumbnails as I have 10,000 images.

Also, for example I cannot see this image when I click on it but I get the following result:

$ identify 590.jpeg
590.jpeg JPEG 450x338 450x338+0+0 8-bit DirectClass 47.8KB 0.000u 0:00.000


>>> from PIL import Image
>>> im = Image.open("590.jpeg")
>>> im.verify()

Update: Image could not open broken png files but cannot detect broken jpg/jpeg files:

>>> im = Image.open("722.png")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/PIL/Image.py", line 2590, in open
    % (filename if filename else fp))
OSError: cannot identify image file '722.png'

722.png is

[jalal@goku media30_GV_Aug2018]$ identify 722.png
722.png SVG 3024x3024 3024x3024+0+0 16-bit DirectClass 2.61KB 0.000u 0:00.009
  • 1
    Those files aren't broken/corrupt/damaged. They're just SVG's with the wrong extension. Change the extension to .svg and then you should be able to open them. – Joseph Sible-Reinstate Monica Sep 28 '18 at 1:45
  • some are broken some are SVG. How can automatically do this? as I said, I have 10,000 images so I cannot go through all images one by one – Mona Jalal Sep 28 '18 at 1:48
  • Can you add an example of one that's actually broken? – Joseph Sible-Reinstate Monica Sep 28 '18 at 1:49
  • The svg you posted is a stylized Y, white or or off-white on a black or dark background. It opens in gwenview, krita, google-chrome and in firefox. – DK Bose Sep 28 '18 at 2:57

Try ImageMagick's identify command. From the man page:

Identify describes the format and characteristics of one or more image files. It will also report if an image is incomplete or corrupt.


$ identify foo.png
identify: NotAPNGImageFile (foo.png).

$ echo $?

An alternative, is to use PIL (Python Imaging Library):

from PIL import Image

im = Image.open("foo.png")

From the documentation:


Attempts to determine if the file is broken, without actually decoding the image data. If this method finds any problems, it raises suitable exceptions. This method only works on a newly opened image; if the image has already been loaded, the result is undefined. Also, if you need to load the image after using this method, you must reopen the image file.


  • so I am not sure what's wrong. I cannot see the image but I see it is jpeg $ identify 590.jpeg 590.jpeg JPEG 450x338 450x338+0+0 8-bit DirectClass 47.8KB 0.000u 0:00.000 – Mona Jalal Sep 28 '18 at 1:08
  • I also tried im.verify on 590.jpeg and I got no error. Do you think something with my Linux might be broken that when I click on the image it does not open? – Mona Jalal Sep 28 '18 at 1:11
  • It could be @MonaJalal, have you tried using different image viewers? Might as well do an fsck just to be sure. – sudo Sep 28 '18 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.