"No such file" error when running an autoscript

993 Views Asked by At

I am working on a project that is suppose to open during start up of the Raspberry Pi. I can open up several other scripts on start up, however the script that I am trying to open during start up does not open.

Note: this code works when i run it through IDLE, but it will not work when i test it through the LX Terminal. When I test the script under auto run script it gives me an error.
Another note: All image files are in the same folder as the script.

Here is my code:

from Tkinter import *
from PIL import Image, ImageTk
from random import randint
import RPi.GPIO as GPIO

root = Tk()
root.overrideredirect(True)
root.geometry("1920x1080+0+0")

image=Image.open('bikebackground.png')
background_image = ImageTk.PhotoImage(image)
background_label = Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

speed = 4

class Biker(Label):
def __init__(self, master, filename):
    im = Image.open(filename)
    seq =  []
    try:
        while 1:
            seq.append(im.copy())
            im.seek(len(seq)) # skip to next frame
    except EOFError:
        pass # we're done

    try:
        self.delay = im.info['duration']/speed
    except KeyError:
        self.delay = 100

    first = seq[0].convert('RGBA')
    self.frames = [ImageTk.PhotoImage(first)]

    Label.__init__(self, master, image=self.frames[0])

    temp = seq[0]
    for image in seq[1:]:
        temp.paste(image)
        frame = temp.convert('RGBA')
        self.frames.append(ImageTk.PhotoImage(frame))

    self.idx = 0

    self.cancel = self.after(self.delay, self.play)

def play(self):
    self.config(image=self.frames[self.idx])
    self.idx += 1
    if self.idx == len(self.frames):
        self.idx = 0
    self.cancel = self.after(self.delay, self.play)

anim = Biker(root, 'racer2.gif')
anim.place(relx=0, rely=0, relwidth=1, relheight=1)

w = Label(root, text= 'You are generating\n%d.%d Watts\nof power' %(randint(10,15),
  randint(0,99)), font=("Helvetica", 50))
w.place(relx=.6, rely=.5, relwidth=.4, relheight=.25)

wh = Label(root, text= 'You have generated\n%d.%d Kilojoules\nof energy' %(randint(0,2),     
  randint(0,99)), font=("Helvetica", 50))
wh.place(relx=.6, rely=.75, relwidth=.4, relheight=.25)
root.mainloop()

In LX Terminal, while testing code here is the error:

pi@raspberrypi ~/bin $ /home/pi/bin/script_auto_run
Doing autorun script...
pi@raspberrypi ~/bin $ Traceback (most recent call last):
 File "/home/pi/New/Display.py", line 10, in <module>
   image=Image.open('bikebackground.png')
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2093, in open
   fp = builtins.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'bikebackground.png'
2

There are 2 best solutions below

0
On

It seems the script is not run from the same folder, so it does not find pictures in the working directory. You should append the absolute path of the folder to the image path:

import os.path

# Get the folder on the current file (/home/pi/New for your case)
SCRIPT_DIR = os.path.basename(__file__)

# and then use os.path.join to concatenate paths
image = Image.open(os.path.join(SCRIPT_DIR, 'bikebackground.png'))

.. or explicitly move in the folder of the script before launching the script.

0
On

Instead of going through the absolute path, I just ran the script through the LXDE environment which allows me to boot on start up with ease. This was done by opening up LX Terminal and doing: sudo nano /etc/xdg/lxsession/LXDE/autostart

Then inputting the following command in the autostart file: @sudo python /home/pi/New/Display.py

Then Rebooted the RPi and it worked.