Page 1 of 1

Compare .png

Posted: 2017-02-06T06:17:51-07:00
by Kenny79
Hi I have 2 folders with 100 .png files in each folder they have all the same name e.g IMAG01.png, IMAG02.pmg etc etc. I need to compare each file with the same names and then out put them to a folder called compared.

I have tried compare folder1/*.png folder2/*.png compared/*.png but this does not seem to work, can anyone help me with this, sorry my command line script is not that good.

Ken.

Re: Compare .png

Posted: 2017-02-06T11:42:24-07:00
by snibgo
I would compare one pair of files at a time, and put that inside a shell "for" loop.

Re: Compare .png

Posted: 2017-02-06T12:07:23-07:00
by fmw42
IM will not compare more that two files at a time. So as user snibgo said, you should create a script loop to compare them pair-wise.

Re: Compare .png

Posted: 2017-02-06T13:54:41-07:00
by Kenny79
Ok thanks, Im just unsure how to write the loop as I've searched and searched but cant seem to find anything.

Re: Compare .png

Posted: 2017-02-06T14:16:53-07:00
by fmw42
What is your IM version and OS/platform? Scripting is different on Windows and Unix systems. Are the filenames the same in each folder? Are you trying to compare every image in folder1 with every image in folder2 or just the same named images?

Re: Compare .png

Posted: 2017-02-06T14:35:02-07:00
by fmw42
In unix, if the compare is between the same named images, then you can do the following. I have not tested this code. This presumes that you have images with no spaces in the names or paths.

Code: Select all

cd path2/folder1
list1=`ls *.png`
cd path2/folder2
list2=`ls *.png`
cd
for img in $list1; do
compare metric rmse path2/folder1/$img path2/folder2/img path2/compared/$img
done
where path2 is your actual full path to the folders.

This assumes IM 6. If using IM 7, change convert to magick and change compare to magick compare.

Re: Compare .png

Posted: 2017-02-06T14:44:21-07:00
by Kenny79
Ok thanks yes I'm using windows CMD will the Unix code you just showed work in a similar way ?

Re: Compare .png

Posted: 2017-02-06T15:45:39-07:00
by fmw42
You can run my unix script on Windows only if you install Cygwin or are using Windows 10, which has an optional unix environment. Otherwise, you would need some IM Windows users to provide the equivalent as a .bat script. Syntax is quite different.

Re: Compare .png

Posted: 2017-02-06T18:10:19-07:00
by Kenny79
Thanks for that !, a friend of mine helped me to write it in Windows and it works really good heres how I got it to work for anyone else needing to know

setlocal enabledelayedexpansion
for /r Images %%i in (*) do (
set file2=%%i

compare %%i "!file2:Images=Images2!" "!file2:Images=compared!"
)


This is written in a .bat file and my folders "Images and Images2 and compared" are all on the desktop

Re: Compare .png

Posted: 2017-02-06T18:12:38-07:00
by Kenny79
I was also trying to figure out how to still compare the files in each folder but only output the images that have differences to the compared folder as this would save space.

Re: Compare .png

Posted: 2017-02-06T18:38:37-07:00
by fmw42
You have to get the compare metric score as a variable and do a conditional test against some threshold value that you set. If it passes then you would write to output. With rmse the best values are the smallest (zero is identical). In unix,

Code: Select all

thresh=0.25  # this is arbitrary, you pick what you want here
cd path2/folder1
list1=`ls *.png`
cd path2/folder2
list2=`ls *.png`
cd
for img in $list1; do
value=`compare metric rmse path2/folder1/$img path2/folder2/img null: 2>&1 | sed -n 's/^.*[(]\(.*\)[)]$/\1/p'`
test=`echo "scale=6; $value <= $thresh" | bc`
if [ $test -eq 1 ]; then 
compare metric rmse path2/folder1/$img path2/folder2/img path2/compared/$img
fi
done
compare returns two values, the first is the raw value, then a space, then a value in parenthesis, which is the value scaled to the range 0 to 1. That is the value I am getting from the sed command. I do not know how to extract that part of the metric in Windows batch syntax.

The other way to do it, is to generate both the score and the image and just delete the image is it does not pass the threshold using the OS to delete the image. So

Code: Select all

thresh=0.25  # this is arbitrary, you pick what you want here
cd path2/folder1
list1=`ls *.png`
cd path2/folder2
list2=`ls *.png`
cd
for img in $list1; do
value=`compare metric rmse path2/folder1/$img path2/folder2/img path2/compared/$img 2>&1 | sed -n 's/^.*[(]\(.*\)[)]$/\1/p'`
test=`echo "scale=6; $value <= $thresh" | bc`
if [ $test -eq 0 ]; then 
rm -f path2/compared/$img
fi
done

Re: Compare .png

Posted: 2017-02-06T19:31:04-07:00
by Kenny79
ok thanks !!, I might set up Cygwin and try it with that.

Re: Compare .png

Posted: 2017-02-08T14:29:04-07:00
by Kenny79
Hi I still cant figure out how to wrtie this in windows cmd does any body know how you would go about this ?

Re: Compare .png

Posted: 2018-03-08T05:33:55-07:00
by tats
Kenny79 wrote: 2017-02-06T18:10:19-07:00 Thanks for that !, a friend of mine helped me to write it in Windows and it works really good heres how I got it to work for anyone else needing to know

setlocal enabledelayedexpansion
for /r Images %%i in (*) do (
set file2=%%i

compare %%i "!file2:Images=Images2!" "!file2:Images=compared!"
)


This is written in a .bat file and my folders "Images and Images2 and compared" are all on the desktop
It work on my script.
Thanks