Compare .png

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
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Compare .png

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare .png

Post by snibgo »

I would compare one pair of files at a time, and put that inside a shell "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: Compare .png

Post 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.
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Re: Compare .png

Post by Kenny79 »

Ok thanks, Im just unsure how to write the loop as I've searched and searched but cant seem to find anything.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare .png

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare .png

Post 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.
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Re: Compare .png

Post by Kenny79 »

Ok thanks yes I'm using windows CMD will the Unix code you just showed work in a similar way ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare .png

Post 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.
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Re: Compare .png

Post 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
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Re: Compare .png

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare .png

Post 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
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Re: Compare .png

Post by Kenny79 »

ok thanks !!, I might set up Cygwin and try it with that.
Kenny79
Posts: 7
Joined: 2017-02-06T06:07:02-07:00
Authentication code: 1151

Re: Compare .png

Post 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 ?
tats
Posts: 1
Joined: 2018-03-08T04:51:51-07:00
Authentication code: 1152

Re: Compare .png

Post 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
Post Reply