Need to add a caption to a jpg, python can't find the image

importImage,ImageDraw,ImageFont, os, glob

list = glob.glob('*.jpg')for infile in list:print infile
    file, ext = os.path.splitext(infile)
    outname = file +"_test.jpg"print outname
    im =Image.open(infile)
    d =ImageDraw.Draw(im)
    f =ImageFont.truetype("Arial.ttf",16)
    d.text((4,0), file, font=f)
    im.save(outname)

It prints out the name of the file, so the file is there - so why can't it open it?

The file exists, but I get the following error: enter image description here

Is there an easier way to add a caption to the bottom of the image?

Thanks to the help of Nick & Manu pointing me in the right dierction, I worked out a solution that works:


import
Image,ImageDraw,ImageFontfrom PIL importImageimport glob, os importTkinterimportImageTkimport fnmatch list = glob.glob('*.jpg') cnt =0for infile in list: cnt +=1print infile file, ext = os.path.splitext(infile) outname = file +"_test.jpg"print outname #im = Image.open(infile) im =Image.open(open(infile,'rb')) d =ImageDraw.Draw(im) f =ImageFont.truetype("c:/windows/fonts/arial.ttf",200) x =10 y =550 d.text((x, y), str(cnt), font=f) im.save(outname)print"==========="
原文地址:https://www.cnblogs.com/jacker1979/p/3761805.html