0

I use a Raspberry Pi 4 B with a touchscreen. As OS I use the Raspberry Pi OS with desktop. I am currently working on a digital assistant. I wrote the code on my windows PC but want to run the assistant on my Raspberry pi. But when I start the program I receive the following error:

pygame.error: Couldn't open image.PNG

As it is a bigger project, I'll try to show you just the code that is relevant for the GUI:

import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([800, 480])
pygame.display.set_caption('P.I.A.')
screen_icon = pygame.image.load('image.PNG')
pygame.display.set_icon(screen_icon)

The image.PNG is in the same directory as the programm itself. What really bothers me is that the code works on my windows PC perfectly and does not work at all on my raspberry pi. I can open the image.PNG with an image viewing program.

I tried switching the PNG to a JPG and I also changed the name of the PNG. Both did not work.

Any ideas how I can solve this problem?

Edit: I now also tried to change the working directory and tried to use os.path.join(). Both did not work. The only difference was that it specified which path the picture was when I used option 2.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Yoda
  • 23
  • 5
  • 1
    You might have better luck with this question on [Raspberry Pi Stack](https://raspberrypi.stackexchange.com/) – DrBwts Dec 21 '20 at 16:02
  • first try with `/full/path/to/image.png`. Later you can search question which how to use something like this `os.path.abspath(os.dirname(__file__))` to get full path to running script and use it to create full paths to images and other resources. – furas Dec 21 '20 at 16:44
  • and check if you really have `'image.PNG'` because `'image.png'` is different name. And check in any image viewer if you can open this file. – furas Dec 21 '20 at 16:46

1 Answers1

0

I'd recommend you try changing from a relative path to an absolute path to the file. Ex: pygame.image.load('/home/user/Desktop/image.PNG')

When working with files in Linux, the standard naming convention is lowercase file extensions: ".png", ".jpg". etc. I'd check that both the file name, and your .load end with a lowercase ".png" .

Also if you're running the script from the command line, make sure you're in the same directory as the script and image. Depending on your setup, if you're in your /home dir, and run a script on your /home/username/Desktop like /home/username/Desktop/coolgame.py then the script may be looking in your /home dir for the file.

If none of that works, the easiest thing to do when debugging file/directory errors is to take a peek at exactly where the program is looking. Before you run image.load, look at all the files in the working dir while you're still in the program. The easiest way to do this is probably os.listdir(dir). I'd recommend something like this:

pygame.display.set_caption('P.I.A.')
import os
print(os.listdir('.'))
screen_icon = pygame.image.load('image.PNG')

Best of luck!