[Python] Saving file to jpg format

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm reading a folder with different image types and if they are not in the jpeg format then to save them in this format.

    But when I try to save them in this format I get an IOError. I've confirmed that I am reading the file contents ok:

    I'm trying to do this using the PIL library. This is the code:

    folder = os.listdir("C:\***\***\***\\abc")
    for infile in folder:
    f,e = os.path.splitext(infile)
    newFile = f + ".jpg"
    if infile != newFile:
    try:
    Image.open(infile).save(newFile)
    print "DONE"
    except IOError:
    print "Cannot convert file", infile
    else:
    print "File: " + f + " is in jpg format"


    Checking output here

    folder= os.listdir("C:\***\***\***\\abc")
    print folder
    for infile in folder:
    print infile


    Currently I get the error message:

    Image.open(infile).save(newFile)
    File "C:\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
    fp = __builtin__.open(fp, "rb")
    IOError: [Errno 2] No such file or directory: 'empire.bmp'


    This is pretty much the code from the resource at: http://effbot.org/imagingbook /introduction.htm

    Thanks

    Continue reading...

Compartilhe esta Página