Last Updated: February 25, 2016
· hoffoo

Check for corrupt image files in Go

Go has a great image library packaged with the standard distribution.

I needed to check for corrupt images in go, you can do that like so:

// importing gif, jpeg, and png for side effects
// if these imports are not there the Decode will not be able
// to detect these filetypes
import (
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"

_, _, err = image.Decode(file)
if err != nil {
    println(f.Name() + " seems corrupt")

Full example to scan for bad images in the current directory: