Page 1 of 1

pygame.error: Couldn't open spot_the_diff.png

Posted: Sat Feb 02, 2019 8:13 pm
by TLCbee
I having a problem with the pygame 'Spot the Difference' project. The image
'spot_the_diff.png' will not open.

The files are in the /home/pi folder. I'm able to click and open both pic flies: rpf.io/spot-pic rpf.io/scary-pic and the rpf.io/scream sound will open and play.

After clicking run the black background appears instead of 'spot_the_diff.png" and the black background remains open. Thank you.

______________________________________________________________________
Shell
________________________________________________________________________
Python 3.5.3 (/usr/bin/python3)
>>> %Run 'Sopt the Difference.py'
Traceback (most recent call last):
File "/home/pi/Python/Sopt the Difference.py", line 9, in <module>
difference = pygame.image.load('spot_the_diff.png')
pygame.error: Couldn't open spot_the_diff.png
>>>

_________________________________
pi@raspberry:~ $ cat /proc/version
Linux version 4.9.0-8-amd64 (debian-kernel@lists.debian.org) (gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1) ) #1 SMP Debian 4.9.130-2 (2018-10-27)


import pygame
from pygame.locals import *
from time import sleep
from random import randrange
pygame.init()
width = pygame.display.Info().current_w
height = pygame.display.Info().current_h
screen = pygame.display.set_mode((width, height))
difference = pygame.image.load('spot_the_diff.png')
difference = pygame.transform.scale(difference, (width, height))
screen.blit(difference, (0, 0))
pygame.display.update()

pygame.quit()

I expect the image ('spot_the_diff.png') to briefly appear on screen. Instead the black background appears on screen and remains open.

Re: pygame.error: Couldn't open spot_the_diff.png

Posted: Sun Feb 03, 2019 1:42 pm
by paddyg
from your error message it sounds like spot_the_diff.png isn't in the same directory as your program file. However once you've fixed that you will find that your program 'drops off the end' i.e. you haven't included any looping mechanism. Have a look at the many pygame examples to see how to do this.

PS use the

Code: Select all

[code]...[/ code]
tags as explained at the top of the forum.

Re: pygame.error: Couldn't open spot_the_diff.png

Posted: Sun Feb 03, 2019 2:13 pm
by rpiMike
Your python code '/home/pi/Python/Sopt the Difference.py' does not appear to be in the /home/pi folder.

So either move the 'spot_the_diff.png' into the /home/pi/Python folder or move your python code into /home/pi or add the full path to your image difference = pygame.image.load('/home/pi/spot_the_diff.png').

Re: pygame.error: Couldn't open spot_the_diff.png

Posted: Tue Feb 05, 2019 7:02 am
by TLCbee
Problem resolved. I moved the 'spot_the_diff.png' into the /home/pi/Python folder. :) Thank you both very much.