1

this is my code :

import Image,glob

files = glob.glob("/small/*.gif") 

for imageFile in files:
    print "Processing: " + imageFile
    try:
        im = Image.open(imageFile)
        im.save( "/small_/", "png" )
    except Exception as exc:
        print "Error: " + str(exc)

but it show error :

  File "f.py", line 13
    im.save( "/small_/", "png" )
     ^
SyntaxError: invalid syntax

so what can i do ,

thanks

updated:

import Image,glob,os

files = glob.glob("small/*.gif") 

for imageFile in files:
    filepath,filename = os.path.split(imageFile)
    filterame,exts = os.path.splitext(filename)
    print "Processing: " + imageFile,filterame
    im = Image.open(imageFile)
    im.save( 'small_/'+filterame+'.png','PNG')
blank
  • 17,852
  • 20
  • 105
  • 159
zjm1126
  • 34,604
  • 53
  • 121
  • 166

4 Answers4

2

Try copy and pasting your code in here back into your editor, it works perfectly fine for me. You seem to have some non-printable characters in there or something similar.

Also, have a look at the PIL documentation, save needs a filename or fileobject, not a folder.

Jacob
  • 41,721
  • 6
  • 79
  • 81
1

This is a python code that converts all.gif files in a folder /gifs/ into .gif.png files:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont #dynamic import


import os
rootdir = 'gifs'
extensions = ('.gif')

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        ext = os.path.splitext(file)[-1].lower()
        if ext in extensions:
            print (os.path.join(subdir, file))

            gif=os.path.join(subdir, file)
            img = Image.open(gif)
            img.save(gif+".png",'png', optimize=True, quality=100)

Source: https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720

a.t.
  • 2,002
  • 3
  • 26
  • 66
0

One thing you should fix is adding a filename to save: im.save("/small_/" + filename_you_make_up + ".png", "png"). That shouldn't be responsible for the syntax error though, but it will fix your next problem.

carlpett
  • 12,203
  • 5
  • 48
  • 82
0

This is a python code that converts a file named a.gif in folder /gifs/ into a.gif.png:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont #dynamic import

gif='gifs/a.gif'
img = Image.open(gif)
img.save(gif+".png",'png', optimize=True, quality=100)

Source: https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720

a.t.
  • 2,002
  • 3
  • 26
  • 66