0

I'm confused to as how I would do this. For example if I am referencing pic0.gif on my computer of course all I have to do for something like

def drawx():
    myImWin = ImageWin('\Python34\images\pic0.gif',300,300)
    Im = FileImage('\Python34\images\pic.gif')
    Im.draw(myImWin)

But this would only work on my computer because I know the directory of the pic0.gif. What would I need to do so that it would automatically seek pic0.gif on a different computer with a different directory?

Sam
  • 31
  • 1
  • 6

3 Answers3

2

Usually you have a folder within your project with resources which include images:

[my_project]
    [images]
        pic0.gif

In your code you determine project home directory:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

Then you get the image file like this:

myImWin = ImageWin(os.path.join(BASE_DIR, r'images/pic0.gif', 300, 300)
warvariuc
  • 57,116
  • 41
  • 173
  • 227
0

If you know where the image you are looking for is relative your python script, for example if the image is bundled with your script, you can retrieve the absolute path to the script and go from there:

import os

# This is the full path of the directory containing this script
dirname = os.path.abspath(os.path.dirname(__file__)) 
# We know that the images are in a folder named 'img'
image_path = os.path.join(dirname, "img", "pic0.gif")

Here we are using the module os.path extensively and the __file__ variable which is explained in this stackoverflow answer.

This has the advantage that it would work not where you launch your python script from.

Community
  • 1
  • 1
El Bert
  • 2,958
  • 1
  • 28
  • 36
-3

YOu can try to play around with os module. I.e. Show full path to current folder:

import os
os.path.curdir
>>> '.'
os.path.abspath(os.path.curdir)
>>> 'C:\\Users\\User\\Desktop'
ljetibo
  • 3,048
  • 19
  • 25