I want to run an opencv-python program at Raspbian startup. The program is to capture images continuously from picamera and do some image processing using opencv and display it in full-screen mode. But it failed to start the program and show some errors in the terminal.

For that I did the following things.

Create a folder named autostart in .config folder, then copy-paste terminal shortcut to this folder. this is done to start the terminal at startup. Then added the command

python filename.py at the end of .bashrc file

Error image

Here is my code:

import cv2
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np

camera = PiCamera()
camera.resolution = (320,240)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size = (320,240))

def multi_clahe(img, num):
    for i in xrange(num):
        img = cv2.createCLAHE(clipLimit=2.0, tileGridSize=
     return img

sharp_kern = np.array((
[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]), dtype="int")

while True:   
    for frame in camera.capture_continuous(rawCapture, format="bgr", 
    image = frame.array 
small = cv2.resize(image, (0,0), fx=1, fy=1) 

gray = cv2.cvtColor(small, cv2.COLOR_BGR2GRAY)

his_img = multi_clahe(gray,4)

#segments = slic(small, n_segments = 100, sigma = 5)    
#mark_boundaries(img_as_float(his_img), segments)

sharp_img = cv2.filter2D(his_img, -1, sharp_kern)

final = cv2.GaussianBlur(his_img, (5,5),0)
    vis = np.concatenate((bod, bod), axis=1)
    cv2.namedWindow("final", cv2.WND_PROP_FULLSCREEN)          
    cv2.setWindowProperty("final", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)

   # cv2.imshow('original',small)
    if cv2.waitKey(50) & 0xFF == ord('q'):


How can I fix this?

  • You really don't want to start anything that uses picamera from .bashrc; that file will be sourced in several places due to auto-login and no more than one process can use the camera at once. I'd follow MatsK's suggestion of using /etc/rc.local (but be careful as your script will be running as root)
    – Dave Jones
    Commented Sep 14, 2017 at 22:27

2 Answers 2


I got the answer,

When I give a delay of 2 seconds, it worked fine.

And I found another solution to start the program at startup. created a script file which contain the program to run at startup.

Add this file in home/pi/.config/lxsession/LXDE-pi/autostart by typing @sh /home/pi/scriptfilename.sh

Actually my script file is placed in /home/pi so that i give the path like this

  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Commented Dec 21, 2019 at 2:21

A program that should run at:

  • OS is booting /etc/rc.local
  • User login /home/USER/.bashrc

And to troubleshoot, pipe the output from your python program to a file and add that to your question.


python filename.py > logfile.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.