I have two similar masked images with transparent backgrounds.
I want to get image difference and expect it as low value as images are similar.
But the result shows huge difference over transparent area which looks just same.
(white means diff)
diff_res = ImageChops.difference(lenna1, lenna2).convert('RGB')
I checked lenna1, lenna2 image difference on some website and it says images are just same except small diffs on boundaries.
I then suspected alpha channel value and converted lenna1, lenna2 into ‘RGB’.
and it shows one’s background as black but the other’s as white.
lenna1 = Image.open('lenna1.png')
lenna2 = Image.open('lenna2.png')
converted_lenna1 = lenna1.convert('RGB')
converted_lenna2 = lenna2.convert('RGB')
converted_lenna1 converted_lenna2
Now I know convert() makes transparent area into black as default. But I still don’t get it.
- Why there’s image difference between transparent area?
- Why some transparent image became white and How can I make my own “will be white” transparent image?
*I also tested with same alpha level using .putalpha()
Thank you for reading this question.
2
Answers
Both images have transparency in them, it’s just that one has white pixels made transparent and the other has black pixels made transparent. Another way of saying the same thing is that the underlying colour of transparent pixels is black in one image and white in the other. You can’t see the difference because they are transparent!
Here is
lenna1
with the alpha layer removed on the left, then the alpha layer itself on the right:And here is
lenna2
with the alpha layer removed on the left, then the alpha layer itself on the right:You can make them the same by finding all the transparent pixels, and making them white like this:
If you want to make the transparent pixels visible blue so you can see them for testing, use:
If you have ImageMagick installed, you can force all transparent pixels to become the colour of your choice, say magenta, in Terminal:
That often means you can improve PNG compression and decrease PNG file sizes by making all transparent pixels black and, as a result, the image is likely to compress much better than if the transparent pixels are all wildly different colours:
In Imagemagick 7, you can do masked compare to ignore regions of the image. (Python Wand 0.5.3 supports Imagemagick 7, though I do not know if it supports the masked compare, yet). Here is an example in command line Imagemagick 7:
White:
Green:
Mask:
Differences would show in the following as red.
If I use your images:
If we compare the whole image including the areas under the transparent regions, then
If we include the alpha channel, then due to the inclusion of two same alpha channels, this decreases the comparison metric, which is misleading, and we get: