Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Diff" an image using ImageMagick with emphasis

Follow up on “Diff” an image using ImageMagick

When you are working with forms (as opposed to images), the changes are very hard to see with this technique. I wanted to show some type of yellow highlight maybe 10-20 pixels "bigger" (padding) around the actual pixels that changed.

So instead of just this

enter image description here

Something more like this

enter image description here

It seems like I am just missing something here in the stack that can make this work.

convert '(' file1.png -flatten -grayscale Rec709Luminance ')' \ '(' file2.png -flatten -grayscale Rec709Luminance ')' \ ... here ? ... '(' -clone 0-1 -compose darken -composite ')' \ ... or here ? ... -channel RGB -combine diff.png

I can also run this as separate commands and does not need to be fast, most of this is going to be run offline.

I also tried studying the technique here (specifically how the thumbnail scaling gives you the effect I want as the pixels get expanded) but this code is using the library instead of the ImageMagick command line tools. Line 248 => "make the red as visible as possible"

https://github.com/vslavik/diff-pdf/blob/master/diff-pdf.cpp#L218

An example form is the standard W-9. I made 2 subtle changes

  • PART II .. moved the 2. left about 2 pixels
  • PART II .. on 4., moved word is to the right 2 pixels

enter image description here

enter image description here

and the compare pumps out this (changes in red that you can barely see)

enter image description here

Thank you for any help

like image 529
Chris Go Avatar asked Mar 28 '18 21:03

Chris Go


1 Answers

You can use -morphology dilate in Imagemagick to increase the size of the red areas. For example, using your two forms:

convert JW0wZ.png 1nHWT.png -compose difference -composite -morphology dilate disk:10 +level-colors black,red result.gif

enter image description here

UPDATE:

If you want the background transparent, then try

convert JW0wZ.png 1nHWT.png -compose difference -composite -morphology dilate disk:10 +level-colors "black,red" -fuzz 20% -transparent black result.png

enter image description here

Or better

convert JW0wZ.png 1nHWT.png -compose difference -composite -morphology dilate disk:10 -alpha copy -background red -alpha shape result2.png

enter image description here

Update 2: Here is how to overlay a 50% yellow marking onto you two originals. Change the value as desired. I create a difference image and dilate the white. Then I create a 50% yellow image. Then I composite each of the originals with the yellow using the difference image as a mask. See https://www.imagemagick.org/Usage/compose/#compose

convert JW0wZ.png 1nHWT.png \
\( -clone 0,1 -compose difference -composite -morphology dilate disk:10 \) \
\( -clone 0 -fill yellow -colorize 100 -channel a -evaluate set 50% +channel \) \
\( -clone 0,3,2 -compose over -composite +write 1.png \) \
\( -clone 1,3,2 -compose over -composite +write 2.png \) \
null:

enter image description here

enter image description here

To view this, if on Unix and have X11 installed, you can do

animate -delay 20 -resize 50% 1.png 2.png
like image 109
fmw42 Avatar answered Oct 03 '22 10:10

fmw42