Problem with Image: Opening a file

mcl mcl.office at googlemail.com
Mon Feb 4 02:51:51 EST 2008


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/xhtml" 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



More information about the Python-list mailing list