Django & PIL image path?

175 views
Skip to first unread message

PB

unread,
Feb 29, 2008, 11:33:47 AM2/29/08
to Django users
Hi,

I have been developing an app that does some simple image processing
with PIL that I want to deploy with django. The code works fine with
the testserver but when I deploy it with apache the code can't find my
images.

ie- the code:

im = Image.open("geoplot/data/blue_marble_world_L.png")

(geoplot is application name)

the error:

[Errno 2] No such file or directory: 'geoplot/data/
blue_marble_world_L.png'

I have tried variations :

im = Image.open("<projectname>/geoplot/data/blue_marble_world_L.png")
m = Image.open("blue_marble_world_L.png")

with the same results,

Ideas?

Thanks,

Peter

Malcolm Tredinnick

unread,
Feb 29, 2008, 11:39:48 AM2/29/08
to django...@googlegroups.com

On Fri, 2008-02-29 at 08:33 -0800, PB wrote:
> Hi,
>
> I have been developing an app that does some simple image processing
> with PIL that I want to deploy with django. The code works fine with
> the testserver but when I deploy it with apache the code can't find my
> images.
>
> ie- the code:
>
> im = Image.open("geoplot/data/blue_marble_world_L.png")

This is a relative filename. Relative to whatever the "current
directory" is at the it is executed and that could be anything. By happy
accident, when you're running "manage.py runserver", the current
directory is such that such relative loading works.

A better approach that has been discussed on this list recently is to
create a setting specifying the base path and then us os.path.join to
connect that setting and you relative paths.

The setting could be constructed from the __file__ variable in the
settings file itself (if all your paths are relative to the settings
file), or it could be based on something in sys.path or whatever you
like. Remember that the settings file is a Python file, so you can do
some computations in there. The only thing you should not do is import
anything related to Django in your settings file (which means don't
import your apps, since they'll import Django and it all goes horribly
downhill from there).

Regards,
Malcolm

--
Many are called, few volunteer.
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages