473,414 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

PIL: Getting a two color difference between images

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 3602
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
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
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.<
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
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'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.
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim Fitzpatrick | last post by:
Hi all I'm fairly new to Python with PIL and I'm having some problems with the BILINEAR/BICUBIC filters when rotating and transforming images. If I use resize() with NEAREST, BILINEAR and...
0
by: Mark 'Kamikaze' Hughes | last post by:
In the new Python game I'm developing, I need to crop out individual tiles from larger tilesets, and maintain transparency. Unfortunately, I've run into major deficiencies in both Tkinter and PIL...
0
by: jean-Baptiste Cazier | last post by:
Sæl ! I am trying to rotate some text and paste it into a larger PIL Image. ( cannot use PyFT for some reasons) However I have a problem of transparency and my new images in a box shape are...
5
by: Doug Tolton | last post by:
Does PIL still not have a Group4 Tiff decoder available? I can load a group4 Tiff just fine, but when I attempt to show it I get: >>> im = Image.open('images/00000001.tif') >>> im.show()...
6
by: rzed | last post by:
I'm using PIL to generate some images which may be rotated at the user's option. When they are rotated, the original image is cropped in the new image (which is fine), and the corners are black...
4
by: Will McGugan | last post by:
Hi, I'm writing an app that downloads images. It rejects images that are under a certain size - whithout downloading them completely. I've implemented this using PIL, by downloading the first K...
2
by: Odalrick | last post by:
I'm making a simple program to crop and scale images, essentially make thumbnails from a user defined subset of the image. I'm planning to use Python Image Library to crop and resize the images,...
1
by: David | last post by:
Hello alll, Apologies if this isn't the correct list. I'm getting my feet wet with the Python Imaging Library (PIL) 1.1.6 on Python 2.5.1. I have played around quite a bit with transparent...
13
by: vaneric | last post by:
hi i have a set of RGB images of diff faces (of people )as a 2 dim numpyarray ...something like threefaces=array(, , ]) where xa1,xa2,xa3 are tuples each representing rgb values of a pixel...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.