Having difficulty using pygame.image.load() [duplicate] - python

This question already has an answer here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Closed 1 year ago.
I am trying to use the pygame.image.load() function to create an image while following a tutorial.
This is the code that I used:
import pygame
pygame.init()
running=True
screen = pygame.display.set_mode((1350, 700))
pygame.display.update()
wheel = pygame.image.load('wheel_image.png')
while running:
screen.fill(0, 0, 0)
for event in pygame.event.get():
if event.type ==pygame.QUIT:
running=False
screen.blit(wheel, (675, 350))
pygame.display.update()
But when I run it, it gives this error message:
line 6, in <module>
wheel = pygame.image.load('wheel_image.png')
pygame.error: Couldn't open wheel_image.png
I have the png file in the same directory as my code, so have I made an error in my code or what could be causing this?

Whether the file can be found depends on the location from where you call your code. That's inconvenient so better calculate the project's base path using the __file__ property:
from pathlib import Path
BASE = Path(__file__).resolve().parent
wheel_path = BASE / 'wheel_image.png'
wheel = pygame.image.load(wheel_path)
If you use an older version of pygame that can't handle Path objects, manually turn it into a string:
wheel_path = str(BASE / 'wheel_image.png')

Related

Why isn't my image loading on pygame on chromebook? [duplicate]

This question already has answers here:
Python - error: couldn't open .png file [duplicate]
(4 answers)
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Closed 10 months ago.
I'm coding in python with pygame, but my images won't open. When I try to load an image, this error message pops up: Exception has occurred: error Couldn't open player0.png. I have looked at other people's solutions, but none of them worked. I have tried converting the image into .bmp and .jpg, but it made no difference and gave me the same error message. I have also tried to put it into a separate folder and do this: img = pygame.image.load('assets/player0.png'), but it made no difference.
This is my code currently:
import pygame,sys
from pygame.locals import *
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800,600))
player = pygame.image.load('player0.png')
while True:
screen.fill((255,255,255))
screen.blit(player, (100,100))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Make sure the path to the image is correct.
Check out this solution already posted.
Python - error: couldn't open .png file

How to get Pygame to open images

I've been trying to find a solution to this problem for a while now: I'm trying to display an image using the Pygame module in python but I always get this error:
File "C:/Users/Brandon/PycharmProjects/UnstableUnicorns/UnstableUnicorns Test.py", line 13, in <module>
Baby_Narwhal = pygame.image.load(r"Baby Narwhal.png").convert()
pygame.error: Couldn't open Baby Narwhal.png
(Yes I know there are spaces but there are a lot of images that I want to display and they all have spaces in their names)
I've tried putting the whole path file, using .convert(), using backslashes (and removing the spaces didn't work either) and adding interpreter paths. I've asked this question multiple times on this site and I haven't gotten a working answer. Help.
import pygame
import os
import sys
from time import sleep
print(os.getcwd())
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
# [...]
Baby_Narwhal = pygame.image.load(r"Baby Narwhal.png").convert()
pygame.init()
xDisplay = 1000
yDisplay = 500
white = (255, 255, 255)
def main():
display = pygame.display.set_mode((xDisplay, yDisplay))
while True:
display.fill(white)
display.blit(Baby_Narwhal, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
main()
I think your solution would to get the path of the image or whatever you need. I had this problem about a month ago, and getting the exact path fixed it for me. You should also verify that you have the correct name of the file in your code. Hope this helps.. Good luck! :)
Example: C:/User/Desktop...
(im a linux guy) I don't usually use Windows so I don't know paths off the top of my head. Additionally, you could try and put your pygame file and image into your same folder and just get the name of your image/file.

pygame can't load image [duplicate]

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 2 years ago.
import pygame, sys
from pygame.locals import *
pygame.init()
window = pygame.display.set_mode((400, 300))
my_image = pygame.image.load(r'/Users/Humberto/documents/python/games/alien')
posX, posY = 200, 150
window.blit(my_image, (posX, PosY))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
I am trying to load an image that is in the same folder and it will return an error saying pygame.error: Couldn't open /Users/Humberto/documents/python/games/alien I have tried writing the .png extension, also writing the whole path using / and // and also tried the raw string. How can I fix the problem?

error: couldn't open .png [duplicate]

This question already has answers here:
pygame.error "couldn't open image.png" only in command prompt
(3 answers)
Closed 2 years ago.
Can someone help me identify where I went wrong? Thanks. I get: pygame.error: Couldn't open space.png. My code is as follows:
import random, math, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("orbit demo")
space = pygame.image.load("space.png").convert()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
#draw backgroud
screen.blit(space, (0,0))
pygame.display.flip()
You may have to do the following:
import pygame
import os
try:
# load from subfolder 'data'
background = pygame.image.load(os.path.join("data","background640x480_a.jpg"))
ball = pygame.image.load(os.path.join("data","snake.gif"))
except:
raise UserWarning, "Unable to find the images in the folder 'data' :-( "
This was obtained from the Python Game Book website. Basically you tell it where to look (in the code above, it is the data folder. If it cannot find it, it reports the User Warning.
So in the example that I'm writing for my game, I have an images folder, so I do this
self.image = background = pygame.image.load(os.path.join("images","starship.png")).convert()
I am a newbie to this, but reading suggests this is how you do it.
EDIT - The other reason could be. Is it the correct format (e.g. png) or have you named the file something else?
Try specifying the full path of the image(s), example: C:/Python34/your_folder/file_name.png
Note: in your python code to refer to a path you can't use "\", must use "/"
the "\" has a specific meaning in python.
For me the answer was I was not running the command from within the folder containing the game file. Both my .py file and .png file are in the top level, so I cd into that and ran and it worked.

Pygame Error: Video System not Initialized [duplicate]

This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
pygame window closes immediatly after opening up
(1 answer)
Closed 1 year ago.
I have used Pygame with python 2.7 before but recently I 'upgraded' to python 3.2. I downloaded and installed the newest version of Pygame which is said to work with this version of python. I have, however, had this rather frustrating error on what should be a simple block of code. The code is:
import pygame, random
title = "Hello!"
width = 640
height = 400
pygame.init()
screen = pygame.display.set_mode((width, height))
running = True
clock = pygame.time.Clock()
pygame.display.set_caption(title)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False
else:
print(event.type)
clock.tick(240)
pygame.quit()
And every single time I run it I get:
17
1
4
Traceback (most recent call last):
File "C:/Users/David/Desktop/hjdfhksdf.py", line 15, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
Why am I getting this error?
if event.type == pygame.quit():
In the line above, you're calling pygame.quit() which is a function, while what you really want is the constant pygame.QUIT. By calling pygame.quit(), pygame is no longer initialized, which is why you get that error.
Thus, changing the line to:
if event.type == pygame.QUIT: # Note the capitalization
Will solve your problem.
It's important to note that pygame.quit() will not exit the program.

Categories