[Tutor] Questions about PIL

Luke Paireepinart rabidpoobear at gmail.com
Thu Nov 9 00:16:31 CET 2006


Chris Hengge wrote:
> I'm trying to figure out how to compare im1 to im2 and recognize the 
> difference. I dont care what the difference is...
>
> something like
>
> if im1 is not im2:
>      print "Not same"
Hey, Chris.
I'm supposing here that you are checking if the images are _visually_ 
different,
not if they are different objects or files.

Remember when we were talking about the VNC?
Specifically, the line
diff = ImageChops.difference(self.prevscr,self.currscr)
that I sent to you?

To sum it up, in the ImageChops module,
(which comes with PIL)
there's a function called difference that returns a list with the 
different pixel values for each coordinate of the image.

I would suggest the following course of action:
1) Check if the file format is the same.  If it's a PNG vs a JPG vs a 
BMP or whatever, the compression routines
will have an effect on the image, so your difference test won't work.
2) Check if the resolution is the same.  If one's 640X480 and the 
other's 800X600, you're going to have a difference.
3) do the following ( or something equivalent):
from ImageChops import difference
alist = difference(image1,image2)
a = [b for b in alist.getdata() if b != (0,0,0)]
if len(a) != 0:
    print "Not the same"
>
> I've tried im.tostring () but that doesn't ever enter the loop either.
I have no idea what 'that doesn't ever enter the loop' means.
>
> *******************************************************************************************
> Second question is this:
> Is there a way to divide the screen so I only grab maybe the lower 
> right 200x200 pixels or some such?
> Or possibly a way to seperate the image into a grid so I could just 
> take the grid I wanted?
You're referring to when you're using ImageGrab.grab() I assume,
but you should have said this.  It's better to be explicit than 
implicit, after all :)

Yes, it's possible to grab only part of the screen.
ImageGrab.grab(), if you read the help information on it,
says that it takes a bbox argument with a default value of none.
so override this value with your own bounding box.
I.E. ImageGrab.grab((0,0,200,200)) will grab a square from the 
upper-left corner of the screen.

Separating the image into a grid would also be quite easy.
If you have an Image instance, just use its crop method to get the area 
you want.
example:
import Image
im = Image.open('test.bmp')
im.crop((0,0,200,200)).save('test.bmp')

Should overwrite the old image with a new one.

Also, note that the code:
ImageGrab.grab().crop((0,0,200,200))
is equivalent to
ImageGrab.grab((0,0,200,200))

In other words,
the ImageGrab always takes  a screenshot of the entire working area.
so if you're expecting this bounding-box to speed anything up, it won't.

HTH,
-Luke

> Thanks!
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list