TLCbee
Posts: 4
Joined: Sat Feb 02, 2019 12:58 pm

pygame.error: Couldn't open spot_the_diff.png

Sat Feb 02, 2019 8:13 pm

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.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

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

Sun Feb 03, 2019 1:42 pm

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.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
rpiMike
Posts: 1391
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

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

Sun Feb 03, 2019 2:13 pm

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').

TLCbee
Posts: 4
Joined: Sat Feb 02, 2019 12:58 pm

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

Tue Feb 05, 2019 7:02 am

Problem resolved. I moved the 'spot_the_diff.png' into the /home/pi/Python folder. :) Thank you both very much.

Return to “Python”