question about image path

Hi, All:
     I have a question about the relative path of image.
     usually when I use an image in a code, if I put the image and
the .py file under the same folder, then the path "imagename" work
fine. And if the image is in other folder, then ".folername
\imagename"also works.
     Then one problem is that for using a package, if some .py file in
that package use images, then if those images are under the same
folder of the package, these .py file can't find those images, no
matter if "imagename", or ".foldername\imagename".
     I don't know why that happens.
     Thanks for any suggestion!

I’m not sure I complete grasp your problem, but I’ll take a stab:

When in open (or read) image files into .py modules, you’re having path issues. You need to keep in mind that the ‘open’ calls (and functions that use them) use paths relative to the running code (e.g. the code that imported your python module).

To allow the images to be found you can

  1. Convert them to .py files (there are examples around that do that). These basically embedded the binary image data into a python module that will use normal python module path resolution

  2. Get the path of your base module and use it for all paths

import os.path

Calculate the base directory (to be used later to get external files)

BASEDIR = os.path.dirname(file)

def readimage(filename):

return open(os.path.join(BASEDIR, 'images', filename), 'rb').read()

Will define a function (readimage) that will get an image file from the ‘images’ directory at the same level as this module.

You can of course, tailor this to any schema you choose.

NOTE: The above code was typed in but not tested. It should be very close to usable.

···

On Sun, Jan 23, 2011 at 01:08, zhengqing zhengqinggan@gmail.com wrote:

Hi, All:

 I have a question about the relative path of image.

 usually when I use an image in a code, if I put the image and

the .py file under the same folder, then the path “imagename” work

fine. And if the image is in other folder, then ".folername

\imagename"also works.

 Then one problem is that for using a package, if some .py file in

that package use images, then if those images are under the same

folder of the package, these .py file can’t find those images, no

matter if “imagename”, or “.foldername\imagename”.

 I don't know why that happens.

 Thanks for any suggestion!

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

zhengqing wrote:

     I have a question about the relative path of image.
     usually when I use an image in a code, if I put the image and
the .py file under the same folder, then the path "imagename" work
fine.

are you using raw image files (*.png, *.gif, etc) or python files created with img2py ?

If the latter (py files), then it follows the regular python importing rules -- i.e it looks on sys.path.

If using raw image files, then all paths are relative to the current working directory -- what that is depends on how you start up your app.

What I suggest is that you either use img2py, or:

put all your images, etc, etc in one place -- a "resources" directory of some sort.

In your app, have the path to that dir set near app startup, and then reference it everywhere you need to load something.

wx provides wx.StandardPaths for finding out "standard" location for finding and storing data on different systems -- I"d use one of them.

- Chris

  And if the image is in other folder, then ".folername

···

\imagename"also works.
     Then one problem is that for using a package, if some .py file in
that package use images, then if those images are under the same
folder of the package, these .py file can't find those images, no
matter if "imagename", or ".foldername\imagename".
     I don't know why that happens.
     Thanks for any suggestion!

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thank you very much for the answer.
now everything works fine.

···

On Jan 23, 10:38 am, Christopher Barker <Chris.Bar...@noaa.gov> wrote:

zhengqing wrote:
> I have a question about the relative path of image.
> usually when I use an image in a code, if I put the image and
> the .py file under the same folder, then the path "imagename" work
> fine.

are you using raw image files (*.png, *.gif, etc) or python files
created with img2py ?

If the latter (py files), then it follows the regular python importing
rules -- i.e it looks on sys.path.

If using raw image files, then all paths are relative to the current
working directory -- what that is depends on how you start up your app.

What I suggest is that you either use img2py, or:

put all your images, etc, etc in one place -- a "resources" directory of
some sort.

In your app, have the path to that dir set near app startup, and then
reference it everywhere you need to load something.

wx provides wx.StandardPaths for finding out "standard" location for
finding and storing data on different systems -- I"d use one of them.

- Chris

And if the image is in other folder, then ".folername

> \imagename"also works.
> Then one problem is that for using a package, if some .py file in
> that package use images, then if those images are under the same
> folder of the package, these .py file can't find those images, no
> matter if "imagename", or ".foldername\imagename".
> I don't know why that happens.
> Thanks for any suggestion!

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Bar...@noaa.gov