Best way to compare two images (Lua API)

What is the best way to determine if two images (instances of Image) are pixel identical? I need this to filter out duplicate frames, like when exporting a spritesheet.

All in all, I compare the width and height of the two images, and if they match, I compare them pixel by pixel until the first discrepancy.