6

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

Chris Go
  • 599
  • 6
  • 19

1 Answers1

7

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
fmw42
  • 46,825
  • 10
  • 62
  • 80
  • cool, then I can probably use something along the lines of `-transparent black -fill "rgba(255, 215, 0, 0.1)" -opaque red` to use this as an overlay on one of the original images for the yellow highlight ... the yellow-ish rgba(...) is showing as opaque and not doing the 10% :( – Chris Go Mar 28 '18 at 21:39
  • See my appended answer. – fmw42 Mar 28 '18 at 22:51
  • thank you @fmw42 ... getting closer. I have to get some time to figure out the overlaying this red with transparent background into the actual image of the form itself and making the red into 30% alpha-channel yellow so you can still see the text below. Thank you for your help! I just realized you are Fred's Imagemagick scripts (nice avatar too) http://www.fmwconcepts.com/imagemagick/morphology/index.php – Chris Go Mar 28 '18 at 23:09
  • See my second UPDATE to show you how to do the yellow overlay. Please consider an up-vote if this has been helpful. – fmw42 Mar 29 '18 at 00:16
  • I need to find a way to buy you a beer or something (I'm serious) ... I really appreciate you taking the time to help me out. I am pretty sure it would've taken me at least 10+ hours to figure this out. I know you are retired and all and not need money but maybe I can send you a check for a couple hundred dollars? – Chris Go Mar 29 '18 at 01:12
  • Is there something special about the source PNGs? Some of the source PNGs I am using are not doing the alpha 50% and some are ... the PNGs are ripped from PDFs using the original SO question. Opening them up in Photoshop and Export as PNG and running it again seems to be doing the trick so must be something on the pdfimages – Chris Go Mar 29 '18 at 01:32
  • I think the source PNGs generated by pdftocairo and pdftoppm are coming out as `png:IHDR.color_type: 2 (Truecolor)` which is causing the overlay to become opaque. I have to convert these into `png:IHDR.color_type: 6 (RGBA)` (currently photoshop is doing this on Export to PNG) using IM – Chris Go Mar 29 '18 at 02:13
  • `convert source.png -type TrueColorAlpha source-alpha.png` fixes the source PNG – Chris Go Mar 29 '18 at 02:22
  • All in one step now ... `convert JW0wZ.png -type TrueColorAlpha 1nHWT.png -type TrueColorAlpha \ \( -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:` ... thank you again so much @fmw42 – Chris Go Mar 29 '18 at 02:30