1

I want to convert a webcam shot in OpenCV to PIL so I can read the image pixel for pixel and get the RGB format.

I usually get the RGBA format when working with PIL, but now I get one integer instead of the four numbers I was expecting.

Consider this code snippet:

cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, my_height)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, my_width)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FORMAT, cv.IPL_DEPTH_32F)
  img = cv.QueryFrame(capture)
...
    index_value = index_value + 1
    picture_time = current_time + variance
    #Give the cv image to PIL
    im = Image.fromstring("L", cv.GetSize(img), img.tostring())
    #Get the color set out of the images
    width, height = im.size
    image_pixels = im.load()
    total_size = width * height
    colors = {}
    for h in range(height):
        for w in range(width):
            print width, height
            detail = image_pixels[w, h]
            #Filter our transparencies and dark colors
            print detail

I get a value of things like 14 when I cover the webcam and 140 when I don't.

How do I properly grab an image from the webcam in a Pythonic way and get the RGB value of each pixel?

jkdev
  • 11,360
  • 15
  • 54
  • 77
Encompass
  • 562
  • 3
  • 13
  • 27
  • 1
    What pixel format are `cv.QueryFrame` + `img.tostring()` returning image data to you in? The `"L"` mode argument you're passing to `Image.fromstring()` is telling it to expect 8-bit black and white pixels. – martineau Mar 28 '13 at 20:43
  • @martineau, I've been looking for that information so I could help answer this question, but it doesn't seem to be documented *anywhere*. – Mark Ransom Mar 28 '13 at 21:13
  • Why are you using old `cv` instead of `cv2`? – Igonato Mar 28 '13 at 22:01
  • @MarkRansom: I had the same problem finding out. If all else fails, perhaps the OP could determine it experimentally by examining the contents of what is being returned after frame-grabbing different solidly colored sheets of paper (ideally Red, Green, & Blue). – martineau Mar 28 '13 at 22:13
  • I decided to try saving the image after I did the conversion. Sure enough, it's grascale. Yes, there is little documentation on what the "L" is. – Encompass Mar 29 '13 at 04:23
  • I changes the "L" to "RGB" and I got a blue image. I suppose that would count as closer. – Encompass Mar 29 '13 at 04:26
  • I tried with another camera and instead of a blue shirt, which I have one, I got an orange shirt. – Encompass Mar 29 '13 at 04:32
  • Related to this question is the list of modes. That is, the "L". http://stackoverflow.com/questions/4199522/list-of-image-modes – Encompass Mar 29 '13 at 04:40

0 Answers0