We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

tanapox
Posts: 2
Joined: Thu May 28, 2020 11:41 am

Picamera capture and get single pixel r,g,b values

Thu May 28, 2020 12:26 pm

Hi, first a disclaimer, i have almost zero knowledge in python, just getting code snippets all around an adding to my project..

My goal is it to use the Pi Camera to capture a screenshot and check the red, green and blue value of a specific pixel.

This is part of the code i used:

from picamera import PiCamera
from io import BytesIO
from PIL import Image

camera = PiCamera()
camera.resolution=(1280,720)

stream =BytesIO()

stream.seek(0)
camera.capture (stream,'jpeg')
im=Image.open (stream)

r,g,b=im.getpixel((x,y))

I'm using this in a Pi Zero W and is really slow.
I tried to skip the jpeg capture and go directly with the rgb format which , i think is the reason why this is so slow, using this system:

import picamera
import picamera.array

with picamera.PiCamera() as camera:
with picamera.array.PiRGBArray(camera) as output:
camera.capture(output, 'rgb')
print('Captured %dx%d image' % (
output.array.shape[1], output.array.shape[0]))

but i have no idea how to get the rgb values from output PiRGBArray

Thanks

Sorry for the bad english..

blimpyway
Posts: 728
Joined: Mon Mar 19, 2018 1:18 pm

Re: Picamera capture and get single pixel r,g,b values

Fri May 29, 2020 10:25 am

Please use code tags to format code.

for your problem, add the line:

Code: Select all

		print("pixel at 100x100", output.array[100,100])
with the same indentation as previouse print.

See the result is a 3 number tuple? these are the rgb values of pixel at line 101 column 101(*) in the captured image.

So the output array is three dimensional:
shape[0] = 480 (lines)
shape[1] = 720 (columns)
shape[2] = 3 (r,g,b values)

Code: Select all

   red_pixel_at_line_50_column_200 = output.array[49,199,0]
  
(*) an array's first index is 0

tanapox
Posts: 2
Joined: Thu May 28, 2020 11:41 am

Re: Picamera capture and get single pixel r,g,b values

Fri May 29, 2020 8:40 pm

Thank you for the answer

Return to “Python”