455,587 Members | 1,580 Online
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

# PIL: Getting a two color difference between images

 P: n/a 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. -- Kevin D. Smith Oct 25 '08 #1
4 Replies

 P: n/a On Fri, 24 Oct 2008 14:51:07 -0500, Kevin D. Smith wrote: 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. -- Kevin D. Smith Use the Image.point() Also, see PIL Handbook: http://www.pythonware.com/library/pil/handbook/ index.htm Oct 25 '08 #2

 P: n/a 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 Oct 25 '08 #3

 P: n/a Kevin D. Smith: >What I want is a two color output image: black where the image wasn'tdifferent, and white where it was different.< Use the ImageChops.difference, which would give a difference image. Then map all colors to white except black using Image.point() Oct 25 '08 #4

 P: n/a On 2008-10-25 12:41:51 -0500, be************@lycos.com said: Kevin D. Smith: >What I want is a two color output image: black where the image wasn'tdifferent, 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. 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. -- Kevin D. Smith Oct 28 '08 #5

### This discussion thread is closed

Replies have been disabled for this discussion.