Detect if two pngs are different

(Context: running authotkey scripts to try and automate some tests. The plan is to take screenshots and then compare them with the “standard” screenshots to determine if the output has changed).

Is there a smart way to check if two pngs are different?

By smart, I mean, besides comparing bytes by byte? (after comparing their size, obviously)

+3
source share
3 answers

Assuming that your PNG files are generated by the same software (different PNG authors can create different files for the same source images, because there are some additional settings), and that they do not write down optional information fragments (this, to my mind, several PNG creators), you can check their bytes for bytes at the file level. The standard way is to calculate your hashes (MD5 or SHA1).

+3
source

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
+1
source

, .

0

Source: https://habr.com/ru/post/1747973/


All Articles