bitwes / Gut

Godot Unit Test. Unit testing tool for Godot Game Engine.
1.87k stars 101 forks source link

Add a way to compare images #439

Open bitwes opened 2 years ago

bitwes commented 2 years ago

This little function could be added to Gut with some modifications:

func compare_pixels(img1, img2):
    var are_the_same = true
    var start_pixel = Vector2(0, 0)
    var end_pixel = Vector2(img1.get_width(), img1.get_height())
    var pixel = start_pixel
    var same_count = 0

    img1.lock()
    img2.lock()
    while(pixel.x < end_pixel.x):
        pixel.y = start_pixel.y
        while(pixel.y < end_pixel.y):
            if(img1.get_pixel(pixel.x, pixel.y) == img2.get_pixel(pixel.x, pixel.y)):
                same_count += 1
            pixel.y += 1
        pixel.x += 1

    img1.unlock()
    img2.unlock()

    var num_pixels = end_pixel - start_pixel
    num_pixels = num_pixels.x * num_pixels.y
    return float(same_count) / float(num_pixels)
tom-knight commented 1 year ago

What about using https://github.com/lihop/godot-pixelmatch?

bitwes commented 1 year ago

That's probably a WAY better approach.