1

I am trying to open a png image that is not in the same folder as python. I dont know what to do even though I set the pathway.

I don't know what to do at all

image1 = pygame.image.load('Libraries\Pictures\pixel6.png')
image1 = pygame.transform.scale(image1, (1000, 500))

It says that the image cannot be opened

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Either a possible duplicate of [Pygame not finding image folder when in subdirectory](https://stackoverflow.com/questions/47577034/pygame-not-finding-image-folder-when-in-subdirectory), or have you tried adding an `.\ ` to your file path? – Mercury Platinum May 22 '19 at 18:13
  • What is the working directory? `import os` `os.getcwd()` – Rabbid76 May 22 '19 at 18:14
  • 2
    If you want to be completely sure, try entering the [absolute path](https://askubuntu.com/questions/337443/what-is-a-full-path-name) of the image. something like this: ```C:\Users\Administrator\Desktop\Python Programs\test_python.py``` – Dan May 22 '19 at 18:18

1 Answers1

2

As Dan pointed out, entering the absolute path of the image could definitely work.

Alternatively, you can navigate up the folder hierarchy from the local directory (the directory in which your code file is stored). Let's say that your filesystem appears as follows:

  • You have a directory, Stuff, that contains two folders, Python and Image.
  • The Python folder contains your code, test_python.py.
  • The Image folder contains your image, pixel16.png.

Then, the command would look something like this:

image1 = pygame.image.load("../Image/pixel16.png")

Because the .. takes you up the file tree from the Python folder to the Stuff folder (because when in a given directory, the .. command takes you to the parent directory), and from there the rest of the path navigates you to the image that you're hoping to access.

Hope this helps!

zen_of_python
  • 157
  • 2
  • 10