0

I'm trying to display an image in python, and whenever I try, I've gotten this error:

Traceback (most recent call last):
  File "C:/Users/Brandon/PycharmProjects/UnstableUnicorns/UnstableUnicorns Test.py", line 15, in <module>
    BabyNarwhal = pygame.image.load(r'CardImages\BasicDeck\BabyUnicorns\Baby Narwhal.png').convert()
pygame.error: Couldn't open CardImages\BasicDeck\BabyUnicorns\Baby Narwhal.png

I agree that the image path is a little convoluted, but I can't seem to find a way to get it to load the file. I've tried putting in the whole file path, I've tried to change the read location, but I can't seem to be able to load this image. I really don't want to have to put the images in the same folder as the code since there are a lot of different images I want to display. Here's my code:

import pygame

pygame.init()

xDisplay = 1000
yDisplay = 500

white = (255, 255, 255)

BabyNarwhal = pygame.image.load(r'CardImages\BasicDeck\BabyUnicorns\Baby Narwhal.png').convert()


def main():
    display = pygame.display.set_mode((xDisplay, yDisplay))

    while True:
        display.fill(white)

        display.blit(BabyNarwhal, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            pygame.display.update()


main()

and here is the full image path:

C:\Users\Brandon\PycharmProjects\UnstableUnicorns\CardImages\BasicDeck\BabyUnicorns

Brandon Li
  • 17
  • 1
  • 7

1 Answers1

0

Using spaces in a filename isn't recommended. And using '/' instead of '\' can resolve the error. So, you should rename your image without spaces, and use :

babynarwhal_img = pygame.image.load(CardImages/BasicDeck/BabyUnicorns/BabyNarwhal.png).convert()

Note : modified the variable name because of the folder with the same name, can cause problems on certains versions.

Gugu72
  • 2,052
  • 13
  • 35