Friday, August 5, 2016

102: About My Grayscale Image Blog Page (#34)

The raspistill command for the Pi camera has 100-ish command line options but (oddly) no way to produce a black and white image file. It took me about 10 minutes to come up with a kludge to convert RGB to grayscale. Then I made a blog post about it.

Here's the mystery: With over 100 posts in this blog what is the enduring allure of post #34? Over 6% of my page views still go to that 2 year old entry. And here I thought I'd written about seemingly more important issues.

Anyway, here's a more interesting snippet of Python for a security cam app:

import subprocess
import os
import StringIO
from PIL import Image

threshold = 10 # interesting pixel value diff
numDiffs = 20  # number of diff pixels to assume motion
wide = 120     # size of test images (arbitrary)
high = 90

# Capture a small test image (for motion detection)
def captureImage():
  command = "raspistill -w %s -h %s -t 5 -e bmp -o -" % (wide, high)
  imageData = StringIO.StringIO()
  imageData.write(subprocess.check_output(command, shell=True))
  im ="L") # convert to B/W!
  buffer = im.load()
  return im, buffer

def compareImage(buff1, buff2): # latest vs. previous image
  changedPixels = 0
  for x in xrange(0, wide):
    for y in xrange(0, high):
      pixdiff = abs(buffer1[x,y] - buffer2[x,y])
      if pixdiff > threshold:
        changedPixels += 1
  return (changedPixels > numDiffs) # True return indicates motion

... (code that calls captureImage & compareImage)

No comments:

Post a Comment