user6142489
user6142489

Reputation: 532

Accessing images in GitHub folders from Python

I have images I would like to use for an application I built via Kivy. There are a bunch of them (50+) so I organized my GitHub folder into two separate folders - 'images' and 'code'.

However, when I run this program locally, they're all sitting in the same directory so I can simply run something like the following:

from PIL import Image
image = Image.open('testimage.png')
image.show()

In my GitHub repo, images is a subfolder of code. What I'm trying to be able to achieve is someone using a makefile I have in the repo to run the entire program and have the images found. Something like this from the command line python3 test_program.py.

However, even though images is a subfolder of code, the following does not work: image = Image.open('images\\testimage.png'). I'm guessing it's because the path is no longer right when cloning the repo and trying to run the program through command line. How can I point to these images in my program if they're in another GitHub folder?

My folder structure looks like this:

+ Main GitHub Repo
    + Code Folder
       - test_project.py
       + Images Folder
          - testimage.png

Alternatively, my professor really wanted the images and code to be on the same level (shown below), but I would just like it to work, so whatever you guys think is easiest to do, I'm willing to shuffle the structure.

+ Main GitHub Repo
    + Code Folder
       - test_project.py
    + Images Folder
       - testimage.png

Not sure I described it in the best way, but if anything is unclear, please let me know and I will try to clarify. Any ideas? Thanks!

Upvotes: 0

Views: 2154

Answers (1)

Shailyn Ortiz
Shailyn Ortiz

Reputation: 766

This should fix it

from PIL import Image
image = Image.open('./Images\ Folder/testimage.png')
image.show()

Also if you change folder names to do not have special characters it will be easier to use, let's say renaming Images Folder to Images_folder

from PIL import Image
image = Image.open('./Images_folder/testimage.png')
image.show()

Upvotes: 2

Related Questions