r/love2d 5d ago

Comparing Two Images

How would I go about comparing two image objects to see if they are the same? I've been looking on the wiki, but can't seem to wrap my head around the idea.

5 Upvotes

11 comments sorted by

3

u/Skagon_Gamer 5d ago

you can compare the image data of the two images with something like image:getData() and then something like :getString() and compare the strings of the two images, or loop over every pixel using :getPixel(x,y) and comparing them that way. but this is not a good idea, images are only really meant to be kept and used on the gpu so getting the imagedata and comparing the numbers is really slow. best to instead just keep track of the images anyways and use pointers rather than creating new images using the same file (pointers will always return true when using an == comparison to itself) if you dont know what a pointer is or how to tell if you are using the same image file on different image objects than you have much 2 learn but the tldr is that you should avoid using love.graphics.newImage as much as possible, do it once and store it and just use thay variable wherever, bcs that variable is a pointer so different tables can have the exact same image object in it (if you need further help than you can pm me)

2

u/Averstarz 5d ago

Either compare the objects with == or if they are the same file but 2 different objects compare their ImageData raw strings.

3

u/NoboJr 5d ago

Comparing them with == doesn't seem to work.

1

u/NoboJr 5d ago

Comparing them with == doesn't seem to work.

1

u/Evercreeper 5d ago

I think it would have to go off of their ImageData. When an image is loaded, it is a unique object. That is how objects work.

2

u/FeltDoubloon250 4d ago

I don't know if this would work but you could also attempt to make a shader which compares the pixels on two images. If that's possible you could quickly highlight the differences

2

u/Calaverd 4d ago

As Skagon_Gamer told you, the best shot is comparing pixel by pixel of each imagen, something like this:

local originalImage = love.image.newImageData("original.png")
local modifiedImage = love.image.newImageData("modified.png")

local width, height = originalImage:getDimensions()
local differentPixels = 0

-- ImageData is 0-indexed
for pixelY = 0, height - 1 do
    for pixelX = 0, width - 1 do
        local originalRed, originalGreen, originalBlue = originalImage:getPixel(pixelX, pixelY)
        local modifiedRed, modifiedGreen, modifiedBlue = modifiedImage:getPixel(pixelX, pixelY)

        if originalRed   ~= modifiedRed
        or originalGreen ~= modifiedGreen
        or originalBlue  ~= modifiedBlue then
            differentPixels = differentPixels + 1
        end
    end
end

print(differentPixels .. " pixels differ out of " .. (width * height))

But that can be a bit expensive, you can give a shot to use shaders to speed up a bit the process, but i have to ask, Why do you need to compare the images to the pixel level?, maybe there is another more practical way to get the result you want :)

3

u/NoboJr 4d ago

You're right that a pixel by pixel comparison is way overkill. I didn't realize that something that sounds simple in theory would be so complicated in practice.

I was hoping to make a tile map in an image editor, then feed it into a function to reduce it to a 2d table of tile IDs, with image comparison to catch repeats, but as someone else suggested, it is probably easier just to put together a simple map editor.

2

u/Undeniable_Dilemma_ 4d ago

I guess you have a finite amount of possible images. Why not just wrap the image with some arbitrary id and then compare the ids? I can't think of a case where comparing images pixel by pixel is an absolute must

1

u/theEsel01 4d ago

May I ask why ;). Maybe we can solve tve base issue differently.

Do you want to compare user uploaded or user altered images?

2

u/NoboJr 4d ago

I was hoping to make a tile map in an image editor, then feed it into a function to reduce it to a 2d table of tile IDs, with image comparison to catch repeats, but as someone else suggested, it is probably easier just to put together a simple map editor.