Been a while since I posted anything here, so I decided to post what I've currently been working on.

I use ImageChops to get a difference between two consecutive frames from my webcam. I would like to replace the pixel values > rgb 50, 50, 50 with 255, 0, 0.

I thought about using im.getdata() but it is too slow for "live feed".
I'm thinking the answer is in Image.point()

A more detailed discription on what I'm doing is found here.

My code so far. Its almost what I need.

from VideoCapture import Device
import pygame, Image, sys, ImageChops, time
from pygame.locals import *

cam = Device()

res = cam.getImage()
res = res.size

pygame.init()
screen = pygame.display.set_mode(res)
pygame.display.set_caption("Predator Vision: by Tech B")

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    #grab two images
    im1 = cam.getImage()
    #you will need to wait a sec before taking another image
    #if not, the screen will remain black and buggy
    time.sleep(.2)
    im2 = cam.getImage()

    #im2-im1 per pixel
    diff = ImageChops.difference(im1,im2)
    
    #convert image to pygame type and then display
    shot = pygame.image.frombuffer(diff.tostring(), res, "RGB")
    screen.blit(shot,(0,0))
    pygame.display.flip()

I think you may use the diff data with the use 'load' to alter the pixels like a array of pixels.

Nice project.

Cheers and Happy coding

Using load is still slow. I know PIL has a method for doing it a lot quicker. The point() is what I need, but I'm having issues on figuring out how to pass it functions, and I don't know anything about look up tables.

Maybe there is a way to create a new image based off of the pixel values?

Can't you separate it by channels (RGB) and treat the data on those channels.

load() did the job. Frame rate is still kinda slow, but I'm at least seeing results.

...
diff = ImageChops.difference(im1,im2)

n = diff.load()
for x in range(0,res[0],2): #every other pixel for speed
    for y in range(0,res[1],2):
        if n[x,y] > (25,25,25):
            n[x,y] = (255,0,0)
        else:
            n[x,y] = (0,0,0)
...

A hint that makes a huge difference in another project was doing like this.

...
diff = ImageChops.difference(im1,im2)

n = diff.load()

GRAY = (25, 25, 25)
RED = (255,0,0)
BLACK = (0,0,0)

for x in range(0, res[0], 2): #every other pixel for speed
    for y in range(0, res[1], 2):
        if n[x,y] > GRAY:
            n[x,y] = RED
        else:
            n[x,y] = BLACK
...

I know it doesn't look that better, and in my case and more variables, but it helped, you can try it.

Cheers and Happy coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.