Segmentation fault while using Raspberry Pi 4 and PiCam

I am working on a project for University, and I am attempting to create a program that takes a picture using the PiCam every 4 seconds or so and then calculates the luminosity of the image.

The programs functions on the first loop, but it breaks during the second loop when it gives a Segmentation Fault

import PIL
from PIL import Image
from picamera import PiCamera
from time import sleep

def take_picture():
    #setting the camera
    camera = PiCamera()
    #resolution to (min = 64x64)(max = 2592, 1944)
    camera.resolution = (64, 64)
    #camera take picture
#END take_picture
def current_average_luma():
    img ="/home/pi/Desktop/image1.jpg") #opens image
    luma=0 #sum of the luma of each pixels
    pixels = img.width*img.height #number of pixels
    for x in range(img.width):
        for y in range(img.height):
            (r, g, b) = img.getpixel((x,y))#get colour touple 
            luma += (0.2126*r + 0.7152*g + 0.0722*b) #calculate luma of RGB data, then add to total
        #END for
    #END for
    img.close()#ensure to properly close the image
    return luma/pixels #return average of all pixels
#END average_luma

while (1):
    print("the average luma of the image is: ", current_average_luma()) #print light value

#END while

I am not sure how to solve this problem, or what is causing it. Any advice is appreciated

enter image description here

Read more here:

Content Attribution

This content was originally published by Miller Timlin at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: