如何使用python pil将gif文件更改为png文件
这是我的代码:
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)
但它显示错误:
File "f.py", line 13
im.save( "/small_/", "png" )
^
SyntaxError: invalid syntax
那么我能做什么,
谢谢
更新:
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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将此处的代码复制并粘贴回编辑器中,它对我来说效果非常好。
你似乎有一些不可打印的字符在那里或类似的东西。
另外,请查看 PIL 文档,
保存< /code> 需要文件名或文件对象,而不是文件夹。
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.这是一个 python 代码,它将
/gifs/
文件夹中的所有.gif
文件转换为.gif.png
文件:来源:https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720
This is a python code that converts all
.gif
files in a folder/gifs/
into.gif.png
files:Source: https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720
您应该解决的一件事是添加一个文件名来
save
:im.save("/small_/" + filename_you_make_up + ".png", "png")
。虽然这不应该对语法错误负责,但它会解决你的下一个问题。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.这是一个 python 代码,它将
/gifs/
文件夹中名为a.gif
的文件转换为a.gif.png
:来源:https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720
This is a python code that converts a file named
a.gif
in folder/gifs/
intoa.gif.png
:Source: https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720