Repairing JPEGs

I discovered a batch of old photos I could no longer open. About 300 files in jpeg format. I tried various programs in Linux and Windows, all to no avail.

My default viewer is Gnome Image Viewer. It is the one viewer that gave me some feedback as to what the problem was.

Gnome Image Viewer

I tried this on the default Windows viewer before diving too deep with Google.


Just for fun, I tried Microsoft Paint.


After exhausting my collection of photo viewing/editing utilities, I turned to my backups only to find them corrupted as well. I really wanted to salvage the photos so I decided to try manually repairing them using a Hex Editor; I used GHex but this should work with any Hex Editor such as Hxd.

Here I've opened the corrupted file, P0001157.jpg, in GHex. Notice the zeros - 00 00 00 - etc. at the beginning of the file. This is the problem. Jpegs always begin with FF D8. So you want to scroll down until you find FF D8. This is the Start Of Image, or SOI.

When you find the FF D8, you want to select and delete everything prior to it. A jpeg always starts with FF D8. The errant data will not necessarily be 00s, many of my corrupt files had other noise before the SOI. Just delete all bytes prior to FF D8 and then save the file.

This is how your jpeg should look after editing, the FF D8 at the zero position. Be sure to save this.

And now, your jpeg should open with your viewer of choice.

THOUGHTS: It's unclear what caused the corrupted files. Since my old backups were also bad, I suspect the damage may have been caused by unplugging an external drive while a disk-write was taking place. I used to be bad about that and have since repented.

Also, even if you can open your photo, there still may be less obvious damage to the files. Jpegs begin with FF D8 and they are supposed to end with FF D9 - End Of Image or EOI. Some of mine didn't have this. I tried arbitrarily appending an FF D9 pair to the end of the file using Ghex, but I got unpredictable results. As a work around, I would open the patched jpeg with photo editing software (I used GIMP) and export it as a jpeg.

I hope this was helpful.

↢ Back to the attic