Batch comparing images in two folders, ignoring size

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
laerm
Posts: 6
Joined: 2016-04-04T07:45:18-07:00
Authentication code: 1151

Batch comparing images in two folders, ignoring size

Post by laerm »

Hi all –

I'm brand-new to IM scripting. I read the Usage pages on comparisons and took a look at some scripts as well as the first several pages of a forum search but didn't see anything really relevant to my issue. (I apologize if I missed something.)

I have two folders of the same set of small bitmap images (they're individual glyphs, actually). They are named identically. I would like to compare them in a batch and create a diff image of where they're different. So I have two problems: first is writing a batch script to grab files of the same name from the two folders and pass them to IM command line. If this is beyond the purview, that's fine, ignore this and I'll figure it out somehow. Second and more importantly is how to get the compare to ignore image size. If the images are different sizes, then this is one of the differences I would definitely like to know about. :)

I found https://stackoverflow.com/questions/513 ... magemagick which yielded this command for me to use

Code: Select all

 compare image1 image2 -compose src diff.png
but it throws the error about the images being a different width or height when I test it.

I would appreciate any help in figuring out this issue. Thanks very much.
Micah
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch comparing images in two folders, ignoring size

Post by snibgo »

Looping though files in a script will depend on the script language. You don't say what language you are using, so advice is difficult. With Windows BAT, for example, you would use a "for" loop.

"Compare" won't ignore image size. If the images are different sizes, it won't compare them. You first need to resize one of the images to the same size as the other. It might be best to resize the larger down to the size of the smaller.

A common compare command is like this:

Code: Select all

compare -metric RMSE in1.png in2.png NULL:
This gives a number (in parentheses) between 0.0 and 1.0. 0.0 means they are identical.

Does that help? You might upload a sample couple of images somewhere like dropbox.com and paste the URLs here.
snibgo's IM pages: im.snibgo.com
laerm
Posts: 6
Joined: 2016-04-04T07:45:18-07:00
Authentication code: 1151

Re: Batch comparing images in two folders, ignoring size

Post by laerm »

Whoops, yes, my mistake to leave that out: I'm working in OS X. I know some things about bash scripting but a solution to this one eludes my immediate thoughts.

Hm, well, I know many are different and I want to demonstrate how they are different, so a result that quantifies the difference isn't going to help me much. If size cannot be ignored, though, I see that ImageMagick won't be able to help me. My images are at most 2 pixels different (out of X or Y dimensions of, respectively, 10 or 18 pixels) and I thought that it would be able to show those two pixel rows or columns *as* the difference. I suppose not.

Thanks very much for the help and prompt response.
Micah
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch comparing images in two folders, ignoring size

Post by snibgo »

IM has a multitude of tools to find differences between images. When they are different sizes, what do you mean by "different"? Perhaps one image appears almost exactly within the other image, but with a few pixels different, and you want to find those. IM can do that (with "-subimage-search").

Example images would help us to be more specific.
snibgo's IM pages: im.snibgo.com
laerm
Posts: 6
Joined: 2016-04-04T07:45:18-07:00
Authentication code: 1151

Re: Batch comparing images in two folders, ignoring size

Post by laerm »

Different sizes:
Image 1 is 19x25 pixels
Image
Image 2 is 16x24 pixels
Image

You see the images are essentially identical. Image 1 has a 1px buffer around it that can be ignored. Any idea on how I can diff a bunch (~3000) small PNGs like this to show differences?

Thanks –
Micah
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch comparing images in two folders, ignoring size

Post by fmw42 »

For unix, assume your images are in in folders test1 and test2 with the same names. I will put the difference images in folder test3. I assume they are offset equally on all sides. If you think the offset is bottom-right, then leave off or change the -gravity appropriately.

Code: Select all

path1="/Users/fred/desktop/test1"
path2="/Users/fred/desktop/test2"
path3="/Users/fred/desktop/test3"
cd $path1
img_list=`ls`
for img1 in $img_list; do
img2=`find $path2 | grep "$img1"`
convert $img1 $img2 -gravity center -compose difference -composite ${path3}/$img1
done
Replace my paths with yours.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch comparing images in two folders, ignoring size

Post by snibgo »

Code: Select all

compare -subimage-search -metric RMSE c100.png c100-T.png NULL:

4729.58 (0.0721688) @ 1,1
This shows there is a very close match at position (1,1). If we want to isolate the difference we can crop the large image at that offset, to the dimensions of the smaller image, and find the difference with the small image.

Code: Select all

convert c100.png -crop 16x24+1+1 +repage c100-T.png -compose Difference -composite c.png
The result is entirely black, but with two non-black pixels that represent the differences. To find the coordinates of those pixels, we can turn black pixels transparent and use "sparse-color:" like this:

Code: Select all

convert c100.png -crop 16x24+1+1 +repage c100-T.png -compose Difference -composite -transparent black sparse-color:

2,14,white 1,15,white
snibgo's IM pages: im.snibgo.com
laerm
Posts: 6
Joined: 2016-04-04T07:45:18-07:00
Authentication code: 1151

Re: Batch comparing images in two folders, ignoring size

Post by laerm »

Hi Fred –

Your script has a lot of potential. The gravity option is new to me. I've been playing around with it for the past few minutes, trying to figure out the best setting for it. When there's only one pixel difference in X or Y for the two images I'm diffing, then centering will never work (or at least I can't imagine how it would work consistently). I'll keep playing with it and hope I find a good setting.

Leaving that aside, is there a way for the final image to show differences? Currently, my results just look like both glyphs stacked on top of each other, white on black. I can't differentiate between the two in that manner. Example follows.
Image

snibgo, I have just seen your post as I was composing this response to Fred. Your suggestion looks quite promising. I get an error in the sparse-color portion, though. Is the syntax

Code: Select all

sparse-color: 2,14,white 1,15,white
? The colon makes it hang on my end and removing the colon attempts to create the diff file with the name 2,14,white.

Thanks again for your help.
Micah
laerm
Posts: 6
Joined: 2016-04-04T07:45:18-07:00
Authentication code: 1151

Re: Batch comparing images in two folders, ignoring size

Post by laerm »

Also, snibgo, would I need to run the first portion of your script, the -subimage-search portion, on every image, and then keep track of where to crop? That doesn't sound like fun on >3000 images. :shock:

Micah
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch comparing images in two folders, ignoring size

Post by snibgo »

This is the command. One line.

Code: Select all

convert c100.png -crop 16x24+1+1 +repage c100-T.png -compose Difference -composite -transparent black sparse-color:
This is the output.

Code: Select all

2,14,white 1,15,white
For your images, perhaps trimming them will remove the need for the subimage-search. Command:

Code: Select all

convert c100.png c100-T.png -trim +repage -compose Difference -composite -transparent black sparse-color:
Output:

Code: Select all

2,11,white 1,12,white
I don't understand your last post. You would put both commands within the same "for" loop.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch comparing images in two folders, ignoring size

Post by fmw42 »

Currently, my results just look like both glyphs stacked on top of each other, white on black. I can't differentiate between the two in that manner. Example follows.
That is because the two images are offset. You need to find the offset. Using compare -metric rmse -subimage-search largeimage smallimage diffimage will do what you want to show the difference. But then you need to find which is smaller and which is larger, because compare requires the large image before the small image.
laerm
Posts: 6
Joined: 2016-04-04T07:45:18-07:00
Authentication code: 1151

Re: Batch comparing images in two folders, ignoring size

Post by laerm »

snibgo wrote: For your images, perhaps trimming them will remove the need for the subimage-search. Command:

Code: Select all

convert c100.png c100-T.png -trim +repage -compose Difference -composite -transparent black sparse-color:
Output:

Code: Select all

2,11,white 1,12,white
I don't understand your last post. You would put both commands within the same "for" loop.
Sorry, I was confused, I thought they were run sequentially: run command 1 to get the dimensions necessary to crop.

I'm going to play with your trim suggestion. That seems like something I need to do for simplicity's sake. I'll report back with my progress.

Thanks –
Micah
Post Reply