首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PIL在python 3中保存图像?

如何使用PIL在python 3中保存图像?
EN

Stack Overflow用户
提问于 2016-03-25 21:51:27
回答 1查看 1.5K关注 0票数 2

我在python 3中使用PIL,在一个程序中对图像进行重新着色。它所做的是,它永久地遍历file.gif的像素值(这些值作为整数返回),然后将每一个像素值添加10个。然后,我希望它保存该文件,然后可以重新打开该文件,以便将其写入tkinter标签(剩下的部分我已经得到了,但我只需要知道保存的内容)。

使用这段代码,我得到了打开图像并显示在窗口中的程序,然后修改像素值,但是它不会改变显示的图像。

代码语言:javascript
复制
from tkinter import *
from PIL import Image
from PIL import Image, ImageTk
import time, sys

def col():
    global count1,count,pix,x,root
    count1+=1
    print("("+str(count1)+")")
    count=-1
    for i in pix:
        count+=1
        #print(i)
        i+=10
        pix[count]=i

    photo = PhotoImage(file="AI.gif")
    x.configure(image=photo)
    root.update()
    root.after(100, col)

root=Tk()

photo = PhotoImage(file="AI.gif")
x=Label(root, compound="top", image=photo)
x.pack(side="right")

img = Image.open("AI.gif")
pix=list(img.getdata())
width=img.size[0]
height=img.size[1]
img.close()

root.geometry((str(width)+"x"+str(height))+"-0+0")
root.update()

count1=0
col()

root.mainloop()

我目前正在使用这张图片:

编辑:@Tadhg麦克唐-詹森我刚刚运行了这个程序,上面有你建议的所有编辑,但有以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\name\Desktop\recolour1.py", line 47, in <module>
    col()
  File "C:\Users\name\Desktop\recolour1.py", line 19, in col
    photo.paste(img)
AttributeError: 'PhotoImage' object has no attribute 'paste'

Edit2:这里是我最新版本的代码,它似乎没有修改tkinter窗口中的图像:

代码语言:javascript
复制
from tkinter import *
from PIL import Image
from PIL import Image, ImageTk
import time, sys

def col():
    global count1,count,pix,x,root,photo
    img = Image.open("AI.gif").convert("RGB")
    pix=list(img.getdata())
    count1+=1
    print("("+str(count1)+")")
    count=-1
    for i in pix:
        count+=1
        #print(i)
        i = tuple(V+100 for V in i)

    img.putdata(pix)
    photo.paste(img)
    root.update()
    img.close()
    root.after(10, col)

root=Tk()
photo = ImageTk.PhotoImage(file="AI.gif")
x=Label(root, compound="top", image=photo)
x.pack(side="right")
img = Image.open("AI.gif").convert("RGB")
width,height=img.size[0],img.size[1]
img.close()
root.geometry((str(width)+"x"+str(height))+"-0+0")
root.update()
count1=0
col()
root.mainloop()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-25 22:14:22

每次你这么做:

代码语言:javascript
复制
PhotoImage(file="AI.gif")

您将再次加载该文件,请注意,该文件在整个过程中从不更改,因此图像永远不会更改。如果您已经用PIL加载了映像,那么可以使用ImageTk.PhotoImage从映像加载数据:

代码语言:javascript
复制
photo = ImageTk.PhotoImage(img)

(在定义img之后一定要这样做),那么您就不需要用以下内容重新打开图像:

代码语言:javascript
复制
photo = PhotoImage(file="AI.gif")
x.configure(image=photo)

相反,您只需要将新的像素数据放入img中,然后使用img中的新数据更新img中的以下内容:

代码语言:javascript
复制
img.putdata(pix)
photo.paste(img)

编辑:为了澄清这里是您的代码与所有我建议的编辑:

代码语言:javascript
复制
from tkinter import *
from PIL import Image
from PIL import Image, ImageTk
import time, sys


def col():
    global count1,count,pix,x,root,img
    count1+=1
    print("("+str(count1)+")")
    count=-1
    for i in pix:
        count+=1
        #print(i)
        i+=10
        pix[count]=i

    #update the data in img and then paste it into photo
    img.putdata(pix)
    photo.paste(img)
    root.update()
    root.after(100, col)

root=Tk()

#load the image before making PhotoImage
img = Image.open("AI.gif")
pix=list(img.getdata())
width=img.size[0]
height=img.size[1]
#img.close() #don't close the image as you won't be able to modify it after closing

# do this part after defining img
photo = ImageTk.PhotoImage(img)
        # ^ use PIL's PhotoImage to use PIL operations on it
x=Label(root, compound="top", image=photo)
x.pack(side="right")

root.geometry((str(width)+"x"+str(height))+"-0+0")
root.update()

count1=0
col()

root.mainloop()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36228629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档