Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter使用PIL将画布另存为图像_Python_Tkinter_Python Imaging Library_Tkinter Canvas - Fatal编程技术网

Python Tkinter使用PIL将画布另存为图像

Python Tkinter使用PIL将画布另存为图像,python,tkinter,python-imaging-library,tkinter-canvas,Python,Tkinter,Python Imaging Library,Tkinter Canvas,我有一个代码,它允许用户在画布上绘制,并将其保存为jpeg文件 如中所述,我尝试使用PIL在画布和内存中并行绘制,以便将其保存为jpeg,而不是postscript。它似乎一直在工作,直到我发现我用PIL保存的一些图像与画布上绘制的图像不一样 我假设从PIL图像绘制模块函数中类似地创建canvas.create_line和draw.line,并应给出类似的输出 以下是问题所在: 例如,当我画一个“T”时,它看起来是正常的(左边是我的画,右边是保存的图像) 但当我画一个“A”时,输出图像似乎有点

我有一个代码,它允许用户在画布上绘制,并将其保存为
jpeg
文件

如中所述,我尝试使用PIL在画布和内存中并行绘制,以便将其保存为
jpeg
,而不是
postscript
。它似乎一直在工作,直到我发现我用PIL保存的一些图像与画布上绘制的图像不一样

我假设从PIL图像绘制模块函数中类似地创建canvas.create_line和
draw.line
,并应给出类似的输出

以下是问题所在:

例如,当我画一个“T”时,它看起来是正常的(左边是我的画,右边是保存的图像)

但当我画一个“A”时,输出图像似乎有点奇怪

这是我当前的代码:

import Tkinter as tk
import Image,ImageDraw

class ImageGenerator:
    def __init__(self,parent,posx,posy,*kwargs):
        self.parent = parent
        self.posx = posx
        self.posy = posy
        self.sizex = 200
        self.sizey = 200
        self.b1 = "up"
        self.xold = None
        self.yold = None 
        self.coords= []
        self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
        self.drawing_area.place(x=self.posx,y=self.posy)
        self.drawing_area.bind("<Motion>", self.motion)
        self.drawing_area.bind("<ButtonPress-1>", self.b1down)
        self.drawing_area.bind("<ButtonRelease-1>", self.b1up)
        self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save)
        self.button.place(x=self.sizex/7,y=self.sizey+20)
        self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear)
        self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)

        self.image=Image.new("RGB",(200,200),(255,255,255))
        self.draw=ImageDraw.Draw(self.image)

    def save(self):
        print self.coords
        self.draw.line(self.coords,(0,128,0),width=3)
        filename = "temp.jpg"
        self.image.save(filename)

    def clear(self):
        self.drawing_area.delete("all")
        self.coords=[]

    def b1down(self,event):
        self.b1 = "down"

    def b1up(self,event):
        self.b1 = "up"
        self.xold = None
        self.yold = None

    def motion(self,event):
        if self.b1 == "down":
            if self.xold is not None and self.yold is not None:
                event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue')
                self.coords.append((self.xold,self.yold))

        self.xold = event.x
        self.yold = event.y

if __name__ == "__main__":
    root=tk.Tk()
    root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10))
    root.config(bg='white')
    ImageGenerator(root,10,10)
    root.mainloop()
将Tkinter作为tk导入
导入图像,图像绘制
类图像生成器:
定义初始值(自、父、posx、posy、*kwargs):
self.parent=parent
self.posx=posx
self.posy=posy
self.sizex=200
self.sizey=200
self.b1=“向上”
self.xold=None
self.yold=None
self.coords=[]
self.drawing\u area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
self.drawing\u area.place(x=self.posx,y=self.posy)
self.drawing\u area.bind(“,self.motion)
self.drawing\u area.bind(“,self.b1down)
self.drawing\u area.bind(“,self.b1up)
self.button=tk.button(self.parent,text=“Done!”,width=10,bg='white',command=self.save)
self.button.place(x=self.sizex/7,y=self.sizey+20)
self.button1=tk.Button(self.parent,text=“Clear!”,width=10,bg='white',command=self.Clear)
self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)
self.image=image.new(“RGB”,(200200),(255255))
self.draw=ImageDraw.draw(self.image)
def保存(自我):
打印self.coords
自绘制线(自坐标,(0128,0),宽度=3)
filename=“temp.jpg”
self.image.save(文件名)
def清除(自):
自绘图区域删除(“全部”)
self.coords=[]
def b1down(自身、事件):
self.b1=“向下”
def b1up(自身、事件):
self.b1=“向上”
self.xold=None
self.yold=None
def运动(自身、事件):
如果self.b1==“向下”:
如果self.xold不是None,self.yold不是None:
创建(self.xold、self.yold、event.x、event.y、smooth='true',width=3,fill='blue'))
self.coords.append((self.xold、self.yold))
self.xold=event.x
self.yold=event.y
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
root.wm_几何体(“%dx%d+%d+%d”%(400,400,10,10))
root.config(bg='white')
图像生成器(根,10,10)
root.mainloop()

我哪里出错了?我该怎么做才能将画布上绘制的完全相同的图片保存为jpeg图像?

我设法解决了我的问题。事实上,我只是在装傻。即使
canvas.create_line
draw.line
具有类似的功能,我也没有使用相同的精确数据来绘制这两幅图像。进行更改后,这是我的工作代码

import Tkinter as tk
import Image,ImageDraw

class ImageGenerator:
    def __init__(self,parent,posx,posy,*kwargs):
        self.parent = parent
        self.posx = posx
        self.posy = posy
        self.sizex = 200
        self.sizey = 200
        self.b1 = "up"
        self.xold = None
        self.yold = None 
        self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
        self.drawing_area.place(x=self.posx,y=self.posy)
        self.drawing_area.bind("<Motion>", self.motion)
        self.drawing_area.bind("<ButtonPress-1>", self.b1down)
        self.drawing_area.bind("<ButtonRelease-1>", self.b1up)
        self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save)
        self.button.place(x=self.sizex/7,y=self.sizey+20)
        self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear)
        self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)

        self.image=Image.new("RGB",(200,200),(255,255,255))
        self.draw=ImageDraw.Draw(self.image)

    def save(self):
        filename = "temp.jpg"
        self.image.save(filename)

    def clear(self):
        self.drawing_area.delete("all")
        self.image=Image.new("RGB",(200,200),(255,255,255))
        self.draw=ImageDraw.Draw(self.image)

    def b1down(self,event):
        self.b1 = "down"

    def b1up(self,event):
        self.b1 = "up"
        self.xold = None
        self.yold = None

    def motion(self,event):
        if self.b1 == "down":
            if self.xold is not None and self.yold is not None:
                event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue')
                self.draw.line(((self.xold,self.yold),(event.x,event.y)),(0,128,0),width=3)

        self.xold = event.x
        self.yold = event.y

if __name__ == "__main__":
    root=tk.Tk()
    root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10))
    root.config(bg='white')
    ImageGenerator(root,10,10)
    root.mainloop()
将Tkinter作为tk导入
导入图像,图像绘制
类图像生成器:
定义初始值(自、父、posx、posy、*kwargs):
self.parent=parent
self.posx=posx
self.posy=posy
self.sizex=200
self.sizey=200
self.b1=“向上”
self.xold=None
self.yold=None
self.drawing\u area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey)
self.drawing\u area.place(x=self.posx,y=self.posy)
self.drawing\u area.bind(“,self.motion)
self.drawing\u area.bind(“,self.b1down)
self.drawing\u area.bind(“,self.b1up)
self.button=tk.button(self.parent,text=“Done!”,width=10,bg='white',command=self.save)
self.button.place(x=self.sizex/7,y=self.sizey+20)
self.button1=tk.Button(self.parent,text=“Clear!”,width=10,bg='white',command=self.Clear)
self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20)
self.image=image.new(“RGB”,(200200),(255255))
self.draw=ImageDraw.draw(self.image)
def保存(自我):
filename=“temp.jpg”
self.image.save(文件名)
def清除(自):
自绘图区域删除(“全部”)
self.image=image.new(“RGB”,(200200),(255255))
self.draw=ImageDraw.draw(self.image)
def b1down(自身、事件):
self.b1=“向下”
def b1up(自身、事件):
self.b1=“向上”
self.xold=None
self.yold=None
def运动(自身、事件):
如果self.b1==“向下”:
如果self.xold不是None,self.yold不是None:
创建(self.xold、self.yold、event.x、event.y、smooth='true',width=3,fill='blue'))
self.draw.line(((self.xold,self.yold),(event.x,event.y)),(0128,0),宽度=3)
self.xold=event.x
self.yold=event.y
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
root.wm_几何体(“%dx%d+%d+%d”%(400,400,10,10))
root.config(bg='white')
图像生成器(根,10,10)
root.mainloop()