# how to handle corrupted graphics file

I am generating a latex file with many `\includegraphics` commands from a program. The image files change from run to run. The latex file might, for example, look like the one in this question. Occasionally an image file that I receive is corrupted. I can eliminate nearly all such situations by checking the size of the image file and emitting some vertical space instead of the `\includegraphics` command should the the image file be too small to reasonably be the sort of image I am expecting; however, a very small number of corrupted image files are larger. In that case pdflatex stops with an error message. All my image files are `.jpg` files. I believe that jpegtran on UNIX will fix most of these and I could pass every jpeg through that but I am concerned about the impact on runtime if I do that and also there could still be a very few that that does not address. Is there a way to handle this? Basically I need something like: "include a jpg file using `includegraphics` without generating an error and if that fails then emit some vertical space otherwise the `includegraphics` can stand".

-
Are your images all in the same folder? I would run some external script that can pick up the corrupted images and replace them with some standard faulty image (like `mwe`'s `example-image-a`). –  Werner Sep 21 '12 at 21:56
I could run `jpegtran` on them all in advance but its not really that convenient and also I am not sure that that would necessarily catch them all although it probably would catch most. I would prefer if all this were self contained in the report so I don't have to start messing with external processes. –  user1189687 Sep 21 '12 at 22:00
TeX can't even adequately handle bounding box issues, so I don't think it would be able to detect corrupted JPGs internally/at compile time (let alone efficiently). –  Werner Sep 21 '12 at 22:07
So there is no try...catch processing possible in tex? Seems like a serious omission. –  user1189687 Sep 21 '12 at 22:55
Perhaps related: Trapping LaTeX error/warning –  Werner Sep 21 '12 at 22:58

``````Error: pdflatex (file corrupt.jpg): reading JPEG image failed
Even with `nonstopmode` active indicates to me that this is a pdfTeX specific issue which occurs in the special PDF-related code of the binary which is responsible for reading JPEG files and not a real TeX error.