如何使用python pil将gif文件更改为png文件

发布于 2024-11-19 17:52:17 字数 820 浏览 2 评论 0 原文

这是我的代码:

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')

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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城旧梦 2024-11-26 17:52:17

尝试将此处的代码复制并粘贴回编辑器中,它对我来说效果非常好。
你似乎有一些不可打印的字符在那里或类似的东西。

另外,请查看 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.

七禾 2024-11-26 17:52:17

这是一个 python 代码,它将 /gifs/ 文件夹中的所有 .gif 文件转换为 .gif.png 文件:

#!/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)

来源:https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720

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

蓦然回首 2024-11-26 17:52:17

您应该解决的一件事是添加一个文件名来saveim.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.

哭了丶谁疼 2024-11-26 17:52:17

这是一个 python 代码,它将 /gifs/ 文件夹中名为 a.gif 的文件转换为 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)

来源:https://gist.github.com/Kennyl/5854a11a0793a90fc8ea6c4746ff9720

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文