PIL: Getting a two color difference between images

K

Kevin D. Smith

I'm trying to get the difference of two images using PIL. The
ImageChops.difference function does almost what I want, but it takes
the absolute value of the pixel difference. What I want is a two color
output image: black where the image wasn't different, and white where
it was different. Right now I get black where it wasn't different, and
abs(image1-image2) where it was different.

It would be nice if I could specify the colors for difference and no
difference. This sounds like it should be easy, but I just don't see
how to do it.
 
L

Lie Ryan

I'm trying to get the difference of two images using PIL. The
ImageChops.difference function does almost what I want, but it takes the
absolute value of the pixel difference. What I want is a two color
output image: black where the image wasn't different, and white where it
was different. Right now I get black where it wasn't different, and
abs(image1-image2) where it was different.

It would be nice if I could specify the colors for difference and no
difference. This sounds like it should be easy, but I just don't see
how to do it.

Use the Image.point()

Also, see PIL Handbook: http://www.pythonware.com/library/pil/handbook/
index.htm
 
B

bearophileHUGS

Kevin D. Smith:
What I want is a two color output image: black where the image wasn't different, and white where it was different.<

There are several ways to do that. If speed isn't essential, then you
can create a third blank image of the right size, and then use the
method that iterates on the pixels of an image, and assign p1 != p2 at
every pixel of the third image.

If speed is important you can copy the images into numpy arrays and
then your operation becomes easy.

Maybe there are built-in ways in PIL too, I don't know. You can also
find an intermediate solution, like computing the difference image
with PIL and then binarize it manually.

Bye,
bearophile
 
L

Lie Ryan

Kevin D. Smith:
Use the ImageChops.difference, which would give a difference image. Then
map all colors to white except black using Image.point()
 
K

Kevin D. Smith

Kevin D. Smith:

There are several ways to do that. If speed isn't essential, then you
can create a third blank image of the right size, and then use the
method that iterates on the pixels of an image, and assign p1 != p2 at
every pixel of the third image.

If speed is important you can copy the images into numpy arrays and
then your operation becomes easy.

Maybe there are built-in ways in PIL too, I don't know. You can also
find an intermediate solution, like computing the difference image
with PIL and then binarize it manually.

This last method is what I ended up doing for now. I use the PIL
differencing function, then walk through the result of getdata() to
binarize it. I was hoping there might be a way to run a filter or
something that might be faster, but I haven't figured it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top