[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Problem with Image: Opening a file

mcl

2/4/2008 7:52:00 AM

I am obviously doing something stupid or not understanding the
difference between HTML file references and python script file
references.

I am trying to create a thumbnail of an existing .jpg file. It is in
the directory 'temp', which is below my script

I can display the file with <img>, but Image.open does not see it.

I think it is probably a path problem, but I have tried '/temp/
fred.jpg' and './temp/fred.jpg'

Can anyone help, please

PS I am using mod_python on a Linux Apache 2 and python 2.5

CODE =================================================

import os
import StringIO

def index(req):
main(req)

def writeHTMLHeader(req):

req.content_type = "text/html"
req.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"...)
req.write('<html xmlns="http://www.w3.org/1999/x... lang="en"
xml:lang="en">')
req.write('<head><title>My Tests: Python</title></head>')
req.write('<body>')

def writeHTMLTrailer(req):
req.write('</body>')
req.write('</html>')

def writelineHTML(req, htmlMSG):
req.write(htmlMSG + '<BR>\n')


def createThumb(req, infile, maxwidth, maxheight):
import Image
import os
infile = "temp/fred.jpg"
outfile = "temp/fred_th.jpg"
if infile != outfile:

writelineHTML(req, "Opening %s" % str(infile))
writelineHTML(req, '<img src="%s"></img>' % infile) # Finds
File OK ***********************
im =
Image.open(infile) #
ERRORS on the same file *********************
im.thumbnail((maxwidth,maxheight),Image.ANTIALIAS)
im.save(outfile, "JPEG")
writelineHTML(req, "Created: " + str(outfile))
writelineHTML(req, '<img src="%s"></img>' % outfile)

return outfile

return ""

def showThumbnail(req, picture):
myThumb = createThumb(req, picture, 60, 60)
if myThumb:
writelineHTML(req, '<img src="%s></img>' % myThumb)
else:
writelineHTML(req, "Thumb %s Not Created" % myThumb)


def main(req):
writeHTMLHeader(req)
picture = "temp/fred.jpg"
showThumbnail(req, picture)
writeHTMLTrailer(req)

ERROR ==============================================

File "/home/mcl/htdocs/timslists/thumbs2.py", line 33, in
createThumb
im = Image.open(infile)

File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1888, in
open
fp = __builtin__.open(fp, "rb")

IOError: [Errno 2] No such file or directory: 'temp/fred.jpg'

Richard
2 Answers

Graham.Dumpleton

2/4/2008 10:44:00 PM

0

On Feb 4, 6:51 pm, mcl <mcl.off...@googlemail.com> wrote:
> I am obviously doing something stupid or not understanding the
> difference between HTML file references and python script file
> references.
>
> I am trying to create a thumbnail of an existing .jpg file. It is in
> the directory 'temp', which is below my script
>
> I can display the file with <img>, but Image.open does not see it.
>
> I think it is probably a path problem, but I have tried '/temp/
> fred.jpg' and './temp/fred.jpg'
>
> Can anyone help, please
>
> PS I am usingmod_pythonon a Linux Apache 2 and python 2.5
>
> CODE =================================================
>
> import os
> import StringIO
>
> def index(req):
> main(req)
>
> def writeHTMLHeader(req):
>
> req.content_type = "text/html"
> req.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//
> EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"...)
> req.write('<html xmlns="http://www.w3.org/1999/x... lang="en"
> xml:lang="en">')
> req.write('<head><title>My Tests: Python</title></head>')
> req.write('<body>')
>
> def writeHTMLTrailer(req):
> req.write('</body>')
> req.write('</html>')
>
> def writelineHTML(req, htmlMSG):
> req.write(htmlMSG + '<BR>\n')
>
> def createThumb(req, infile, maxwidth, maxheight):
> import Image
> import os
> infile = "temp/fred.jpg"
> outfile = "temp/fred_th.jpg"
> if infile != outfile:
>
> writelineHTML(req, "Opening %s" % str(infile))
> writelineHTML(req, '<img src="%s"></img>' % infile) # Finds
> File OK ***********************
> im =
> Image.open(infile) #
> ERRORS on the same file *********************
> im.thumbnail((maxwidth,maxheight),Image.ANTIALIAS)
> im.save(outfile, "JPEG")
> writelineHTML(req, "Created: " + str(outfile))
> writelineHTML(req, '<img src="%s"></img>' % outfile)
>
> return outfile
>
> return ""
>
> def showThumbnail(req, picture):
> myThumb = createThumb(req, picture, 60, 60)
> if myThumb:
> writelineHTML(req, '<img src="%s></img>' % myThumb)
> else:
> writelineHTML(req, "Thumb %s Not Created" % myThumb)
>
> def main(req):
> writeHTMLHeader(req)
> picture = "temp/fred.jpg"
> showThumbnail(req, picture)
> writeHTMLTrailer(req)
>
> ERROR ==============================================
>
> File "/home/mcl/htdocs/timslists/thumbs2.py", line 33, in
> createThumb
> im = Image.open(infile)
>
> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1888, in
> open
> fp = __builtin__.open(fp, "rb")
>
> IOError: [Errno 2] No such file or directory: 'temp/fred.jpg'
>
> Richard

Code under mod_python will typically run as Apache user. This means
that when writing files the location you use must be writable to the
Apache user. Do note though that the working directory of Apache is
not guaranteed and you must always use an absolute path to the
location you are saving files, you cannot reliably use relative paths
like you are.

BTW, do you perhaps mean '/tmp/fred.jpg'? There is no '/temp'
directory on Linux boxes.

Graham

mcl

2/5/2008 10:03:00 AM

0

On Feb 4, 10:43 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:
> On Feb 4, 6:51 pm,mcl<mcl.off...@googlemail.com> wrote:
>
>
>
> > I am obviously doing something stupid or not understanding the
> > difference between HTML file references and python script file
> > references.
>
> > I am trying to create a thumbnail of an existing .jpg file. It is in
> > the directory 'temp', which is below my script
>
> > I can display the file with <img>, but Image.open does not see it.
>
> > I think it is probably a path problem, but I have tried '/temp/
> > fred.jpg' and './temp/fred.jpg'
>
> > Can anyone help, please
>
> > PS I am usingmod_pythonon a Linux Apache 2 and python 2.5
>
> > CODE =================================================
>
> > import os
> > import StringIO
>
> > def index(req):
> > main(req)
>
> > def writeHTMLHeader(req):
>
> > req.content_type = "text/html"
> > req.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//
> > EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"...)
> > req.write('<html xmlns="http://www.w3.org/1999/x... lang="en"
> > xml:lang="en">')
> > req.write('<head><title>My Tests: Python</title></head>')
> > req.write('<body>')
>
> > def writeHTMLTrailer(req):
> > req.write('</body>')
> > req.write('</html>')
>
> > def writelineHTML(req, htmlMSG):
> > req.write(htmlMSG + '<BR>\n')
>
> > def createThumb(req, infile, maxwidth, maxheight):
> > import Image
> > import os
> > infile = "temp/fred.jpg"
> > outfile = "temp/fred_th.jpg"
> > if infile != outfile:
>
> > writelineHTML(req, "Opening %s" % str(infile))
> > writelineHTML(req, '<img src="%s"></img>' % infile) # Finds
> > File OK ***********************
> > im =
> > Image.open(infile) #
> > ERRORS on the same file *********************
> > im.thumbnail((maxwidth,maxheight),Image.ANTIALIAS)
> > im.save(outfile, "JPEG")
> > writelineHTML(req, "Created: " + str(outfile))
> > writelineHTML(req, '<img src="%s"></img>' % outfile)
>
> > return outfile
>
> > return ""
>
> > def showThumbnail(req, picture):
> > myThumb = createThumb(req, picture, 60, 60)
> > if myThumb:
> > writelineHTML(req, '<img src="%s></img>' % myThumb)
> > else:
> > writelineHTML(req, "Thumb %s Not Created" % myThumb)
>
> > def main(req):
> > writeHTMLHeader(req)
> > picture = "temp/fred.jpg"
> > showThumbnail(req, picture)
> > writeHTMLTrailer(req)
>
> > ERROR ==============================================
>
> > File "/home/mcl/htdocs/timslists/thumbs2.py", line 33, in
> > createThumb
> > im = Image.open(infile)
>
> > File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1888, in
> > open
> > fp = __builtin__.open(fp, "rb")
>
> > IOError: [Errno 2] No such file or directory: 'temp/fred.jpg'
>
> > Richard
>
> Code under mod_python will typically run as Apache user. This means
> that when writing files the location you use must be writable to the
> Apache user. Do note though that the working directory of Apache is
> not guaranteed and you must always use an absolute path to the
> location you are saving files, you cannot reliably use relative paths
> like you are.
>
> BTW, do you perhaps mean '/tmp/fred.jpg'? There is no '/temp'
> directory on Linux boxes.
>
> Graham

Graham,

Thanks for the explanation.

The answer is/was

Python script os.path.dirname(__file__) + '/mydir/temp/fred.jpg'
HTML 'temp/fred.jpg'

Thanks again

Richard