tags:

views:

50

answers:

3

(Context: running autohotkey scripts to try and automate some tests. The plan is to take screenshots and then compare them to 'standard' screenshots to detect if output has changed).

Is there a 'clever' way to check if two png images are different?

By clever I mean other than comparing them byte by byte? (after having compared their size, obviously)

A: 

You could hash the standard screenshots and then compare that hash to the new screenshot hash.

Gonzalo Quero
+2  A: 

Assumming that your PNG files are generated by the same software (different PNG writers could create different files for same original images, because there are some optional settings) and that they dont write the time related optional informational chunks (few PNG creators do that, I believe) you can check them byte by byte, at the file level. The standard way is to compute their hashes (MD5 or SHA1).

leonbloy
Hm, looks like Bitmap.Save to png *does* fiddle with something (if I do two screen shots in a row, the binary is different :( Think I might have to go for Bitmap.GetPixel...
Benjol
Perhaps it is saving time creation ? You can check it by opening it in a binary/hex viewer and searching for "time" (case insensitive).You can also try to save in other format, perhaps BMP - it's more simple.
leonbloy
A: 

My current implementation, works for me, but a bit slow (especially if they are the same):

open System.Drawing

let aresame fp1 fp2 =
    let bitmap (f:string) = new Bitmap(f)

    let same (bm1:Bitmap) (bm2:Bitmap) =
        if bm1.Size <> bm2.Size then
            false
        else 
            seq { for x = 0 to bm1.Width - 1 do
                    for y = 0 to bm1.Height - 1 do
                        yield bm1.GetPixel(x, y) = bm2.GetPixel(x, y) } 
            |> Seq.forall id

    use bm1 = bitmap fp1
    use bm2 = bitmap fp2
    same bm1 bm2
Benjol