How to save the DIFFERENCES between two images ?

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

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 »

Perhaps hypersnat and your other applications cannot truly display transparency and flatten the transparency onto a black background. For example Paint is a very very old application and may not truly be able to handle transparency, but when it sees it, it flattens the transparency over a black background to remove the transparency channel and replace the background image with black. It is similar to converting what is under the transparency into black and getting rid of the transparent channel.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to save the DIFFERENCES between two images ?

Post by snibgo »

When I run the convert command given by fmw42, I get a different image to image12diff.png on Google. (Personally, I'd put a +repage after the +trim. But that isn't relevant to transparency.)

This is using an old IM: 6.4.5 Q16.

The image I get has a transparent background, which I would expect. When I un-erase it with Gimp, it reveals the original green grass etc, as I would expect. The version of that file on Google doesn't have transparency: there is a white background, which I wouldn't expect, but explains why programs don't show transparency.

minglw: What version of IM are you running?
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: How to save the DIFFERENCES between two images ?

Post by fmw42 »

snibgo wrote:When I run the convert command given by fmw42, I get a different image to image12diff.png on Google. (Personally, I'd put a +repage after the +trim. But that isn't relevant to transparency.)
Yes, I was going to make a point about that, but forgot.

If you want to keep the virtual canvas info so it can be placed back exactly in the other image (it uses the virtual canvase to remember where it came from relative to the original image), then leave off the +repage.

But if you want the cleanest result with no "memory" of where it came from, then add +repage.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to save the DIFFERENCES between two images ?

Post by glennrp »

I can't say why some applications display the image properly and some don't, but I can tell you what's different. image12diff.png image is an RGBA PNG (i.e., PNG colortype 6, with alpha channel), while image12diffx_Hypersnap_saved.png image is an RGB PNG with a tRNS chunk (i.e., PNG colortype 2, with a single color marked transparent).

Both are spec-compliant PNGs. I guess some applications still don't handle all spec-compliant PNG files correctly.

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

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 »

glennrp wrote:I can't say why some applications display the image properly and some don't, but I can tell you what's different. image12diff.png image is an RGBA PNG (i.e., PNG colortype 6, with alpha channel), while image12diffx_Hypersnap_saved.png image is an RGB PNG with a tRNS chunk (i.e., PNG colortype 2, with a single color marked transparent).

Both are spec-compliant PNGs. I guess some applications still don't handle all spec-compliant PNG files correctly.

Glenn

Glenn,

Does that mean he can get what he wants by adding

-alpha on -define png:color-type=2 at the end before his output image (or something like this)? or is that special transparency chuck not supported in IM for RGB formats?
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw »

snibgo wrote:minglw: What version of IM are you running?
I am using this package: ImageMagick-6.5.9-6
running it on WinXP with service pack 2.
I downloaded this zip: ImageMagick-6.5.9-Q16-windows.zip

Thanks to Glenn for explaining the differences between the two PNG files!

Does that mean there are different ways to mark transparency in PNG ?
(and we know that not all PNG viewers can display all of them properly.)

fmw42 wrote: Does that mean he can get what he wants by adding

-alpha on -define png:color-type=2 at the end before his output image (or something like this)? or is that special transparency chuck not supported in IM for RGB formats?
I tried with "-alpha on -define png:color-type=2" added before -trim and the result is this:
https://docs.google.com/leaf?id=0B-jgOG ... MGM2&hl=en

Did I do that right? (perhaps the options should be in a different place?)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 »

I tried with "-alpha on -define png:color-type=2" added before -trim and the result is this:
https://docs.google.com/leaf?id=0B-jgOG ... MGM2&hl=en

Did I do that right? (perhaps the options should be in a different place?)
Moving it won't matter. Apparently it is not implemented in IM with the transparency. I was just asking Glenn to confirm, but I had a feeling that it was not yet implemented.

Post a suggestion to the developers forum to ask that that mode of PNG transparency be added. Don't know what the response will be, but at least you get it on the TO DO list.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to save the DIFFERENCES between two images ?

Post by glennrp »

Before ImageMagick can write an image as an RGB with tRNS (color-type 2), it must meet certain qualifications:
1. Only alpha=0 and alpha=255 can be present (when depth is 8) ... The smiley face there was supposed to be an 8 followed by a ")". Grrr. Let's try that again: "(when depth is eight)"
2. All pixels with alpha=0 must have the same RGB values -- that is, they all must have the same underlying color.
3. There must be no opaque pixels with the same RGB values as the transparent pixels. Oops, your example image12diff.png doesn't meet this one. The other example does.

After you have decided on a nice color to assign to all the transparent pixels (magenta, for example, might be a good choice), the steps that your commandline will have to

1. Convert the transparency to binary (all alpha being 0 or 255) by thresholding or dithering.
2. Convert any magenta pixels to something nearly magenta (e.g., change the blue component from 255 to 254 in any magenta pixels).
3. Convert all transparent pixels to magenta.
4. Write the PNG.

Or something along those lines.


Glenn
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to save the DIFFERENCES between two images ?

Post by glennrp »

It is the *second* criterion that the image doesn't meet. The transparent pixels have lots of different (invisible) colors.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 »

Glenn,

It looks like there is no black in image2.png.

So I did this to make the alpha 0 undercolor all black

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
\( -clone 0 -clone 2 -compose multiply -composite \) \
-delete 0,1 +swap -alpha off -compose copy_opacity -composite -trim +repage \
image12diff2a.png

Image

which looks normal.

But if I add -define, then I get something weird.

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
\( -clone 0 -clone 2 -compose multiply -composite \) \
-delete 0,1 +swap -alpha off -compose copy_opacity -composite -trim +repage \
-define png:color-type=2 image12diff2b.png

Image


So are either of these working to create the special alpha type? Or do I misunderstand? Need I just keep the black background and not overlay the transparency channel? If so, then how does it know that I want black as transparent. I guess I am misunderstanding something.

What about this:

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -compose multiply -composite \
-define png:color-type=2 -trim +repage image12diff2c.png

Image

How does this know that we want black as transparent?

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

Re: How to save the DIFFERENCES between two images ?

Post by snibgo »

How does this know that we want black as transparent?
I'm guessing, but see command-line-options, for -transparent-color:
"The default transparent color is #00000000, which is fully transparent black."
snibgo's IM pages: im.snibgo.com
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw »

Thanks Fred!

The cmd for creating "image12diff2a.png" works great.
This is originally what I wanted. The resulting image has the expected transparent background in all of the PNG viewers I've tried.

Do you mind explaining the the cmd in detail ?

I tried looking up the options and I am still not understanding the details.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 »

minglw wrote:Thanks Fred!

The cmd for creating "image12diff2a.png" works great.
This is originally what I wanted. The resulting image has the expected transparent background in all of the PNG viewers I've tried.

Do you mind explaining the the cmd in detail ?

I tried looking up the options and I am still not understanding the details.

Glad that worked.

convert image2.png image1.png -alpha off +repage \ <--- read images and turn off any existing alpha channel and remove virtual canvas using +repage so that the image sizes are the true canvas sizes
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \ <--- copy the two input images, get the absolute difference image and threshold to black/white so that white is any difference and black is no change. This becomes a mask.
\( -clone 0 -clone 2 -compose multiply -composite \) \ <--- multiply the mask against a copy of image2 so that the difference areas remain and the rest is turned black, which is the color I deduced was not a current color in the image any where.
-delete 0,1 +swap -alpha off -compose copy_opacity -composite -trim +repage \ <--- delete the original two images, turn alpha off, swap the order as needed for the compose, then put the mask as the alpha (trasparency) channel of the image with the changes surrounded by black, trim and reset the virtual canvas (typical when trimming)
image12diff2a.png <--- write output image

Now if your image has black in it, then if I understand Glenn correctly, we would have to make the black some other color. Here is how to do that. Look carefully at the resulting image or the area that is changed or the histogram and see if you can tell some color that is not there. In this case, it looks like there is no pure blue in that region. The we modify the script as follows (see the marked change line).

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
\( -clone 0 -clone 2 -alpha off -compose copy_opacity -composite -compose over -background blue -flatten \) \ <--- rather than multiply the mask with image2 copy, we put the mask into the alpha channel, then flatten that over a blue background, which replaces the transparency with blue.
-delete 0,1 +swap -alpha off -compose copy_opacity -composite -trim +repage \
image12diff2d.png

Image

See if this one works for you.



Just to show you the image under the transparency I will turn off the transparency as follows:

convert image12diff2d.png -alpha off image12diff2d_aoff.png

Image


An alternate approach that creates the same result as image12diff2d.png which is a bit simpler is as follows:

convert image2.png image1.png -alpha off +repage \ <--- same as above
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \ <--- same as above
\( -clone 0 -clone 2 -alpha off -compose copy_opacity -composite -compose over -background blue -flatten \) \ <--- same as above
-delete 0-2 -transparent blue -trim +repage \ <--- delete original images and differnce image, then just make blue into transparent, trim and reset the virtual canvas.
image12diff2e.png

Image
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw »

Hi Fred, thanks for the detailed explanation!

I tried the commands to generate image12diff2d.png and image12diff2e.png.
The result is a bit strange. It's actually different than image12diff2a.png.

For image12diff2a.png, all PNG viewers I've tried can view it properly.

For image12diff2d.png and image12diff2e.png, MS paint didn't display it properly, instead, shows the background as blue.
However, Hypersnap was able to view both properly as transparent background.

Since MS paint is old I don't care about it much. I am happy to know that the cmds for a, d and e works for HyperSnap.
Thank you very much again!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 »

the old tools probably only work with black background under the transparency.

glad it worked for your hypersnap tool.

interesting kind of png

but thanks goes to glennrp for his work on png for IM and for his explanation.
Post Reply